﻿
//设计器的全局变量
//var stopsave = 0;
//var startdrag = 0;

//文本框的类定义
var TextProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
    //可以继续加
}
//按钮的类定义
var ButtonProp = function () {
    this.EName = null;
    this.Class = "btn btn-success";
    this.Text = "按钮";
    this.Size = "btn-default";   //默认正常大小的按钮
    //可以继续添加
}
//容器框的类定义
var DivProp = function () {
    this.EName = null;
    this.Class = "panel panel-default";
}

//面板的类定义
var PanelProp = function () {
    this.EName = null;
    this.CName = "面板";
    this.Class = "panel panel-default";
}

//标签页的类定义
var TabPanelProp = function () {
    this.EName = null;
    this.ActivePage = null;
}

//表格的类定义
var TableProp = function () {
    this.EName = null;
    this.CName = "表格";
    this.Class = "panel panel-default";
    this.row = "2";
    this.col = "2";
}

var TableView = function () {
    this.EName = null;
}


//标签的类定义
var LabelProp = function () {
    this.EName = null;
    this.CName = "文本标签";
}

//分割线的类定义
var LineProp = function () {
    this.EName = null;
    this.CName = "分割线";
}

//数字框的类定义
var NumberProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}
//日期框的类定义
var DateProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}
//时间框的类定义
var TimeProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}
//单选按钮的类定义
var RadioProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.SName = null;
    this.Value = null;
    this.HVstyle = 1; //默认为竖版显示
    this.Expression = null;
}
//复选框的类定义
var CheckboxProp = function () {
    this.EName = null;
    this.CName = null;
    this.DataFieldName = null;
    this.Code = null;
    this.SName = null;
    this.Value = null;
    this.HVstyle = 1; //默认为竖版显示
    this.Expression = null;
}
//图片按钮的类定义
var ImgProp = function () {
    this.EName = null;
    this.Url = ""; //初始化图片路径
    this.Class = 0;
}
//图形的类定义
var ShapeProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.Sstyle = "1"; //默认为圆形
    this.LWidth = "1"; //默认线宽
    this.LineColor = "#000";
    this.ShapeColor = "#fff";
}
//下拉框的类定义
var SelectProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}
//邮箱的类定义
var EmailProp = function () {
    this.EName = null;
    this.CName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.Expression = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}
//字典表的类定义
var DictionaryProp = function () {
    this.EName = null;
    this.Code = null;
    this.DataFieldName = null;
    this.CName = null;
    this.Expression = null;
    this.SingleSelect = 1;  //默认单选
    this.DicTableID = null;
    this.DicTableName = null;
    this.LineColor = "rgb(204, 204, 204)";
    this.ShapeColor = "rgb(255, 255, 255)";
    this.FontColor = "rgb(204, 204, 204)";
    this.FontFamily = "SimSun";
    this.FontStyle = "1";
    this.FontSize = "14";
}

//保存各种控件的数组
var TextArr = new Array();
var ButtonArr = new Array();
var DivArr = new Array();
var LabelArr = new Array();
var NumberArr = new Array();
var DateArr = new Array();
var TimeArr = new Array();
var RadioArr = new Array();
var CheckboxArr = new Array();
var PanelArr = new Array();
var ImgArr = new Array();
var SelectArr = new Array();
var EmailArr = new Array();
var DictionaryArr = new Array();
var TabPanelArr = new Array();
var UnitArr = new Array();
var TableArr = new Array();
var LineArr = new Array();
var ShapeArr = new Array();

var rowFields = new Array(); //行名
var colFields = new Array(); //列名
var mainContent = new Array();

var Tables = new Array();  //存储表格
var Inputs = new Array(); //存储输入控件

////用于存储表格
var TableField = function () {
    //this.ret = null;
    this.colFields = new Array();  //保存列名
    this.rowFields = new Array();  //保存行名
    this.mainContent = new Array();  //保存主体
}

var InputObj = function () {
    this.fcArrObj = null;
}

var curControlArr = new Array(); //存放全部控件
var parCurC = function () {
    this.isTable = null;
    this.curCurC = new Array();
}

//定义的一些全局方法

//初始化当前容器的属性
var InitCurC = function () {
    $("#mainContent").find("[IsContainer=true]").css("background-color", "white");
    $("#mainContent").find("[IsContainer=true]").removeAttr("IsContainer"); //移除该自定义属性
}

//递归得到Left,Top
var GetInnerCPos = function (parCurC) {
    var cLeft = parCurC.css("left").replace("px", "");
    var cTop = parCurC.css("top").replace("px", "");

    if (parCurC.parent().attr("controlType") == "div") {
        var tPos = GetInnerCPos(parCurC.parent());
        cLeft = parseInt(cLeft) + parseInt(tPos.split(",")[0]);
        cTop = parseInt(cTop) + parseInt(tPos.split(",")[1]);
    }
    return cLeft + "," + cTop;
}


//得到URL中传入的参数，param为参数的key
function GetParameter(param) {
    var ret = "";
    var ParamStrArr = location.href.split("?");
    if (ParamStrArr.length > 1) {
        var ParamsArr = ParamStrArr[1].split("&");
        if (ParamsArr.length > 0) {
            for (var i = 0; i < ParamsArr.length; i++) {
                var keyValueDic = ParamsArr[i].split("=");
                if (keyValueDic.length > 0) {
                    var ParamKey = keyValueDic[0];
                    if (ParamKey.toLowerCase() == param.toLowerCase()) {
                        ret = keyValueDic[1];
                        return ret;
                    }
                }
            }
        }
    }
    return ret;
}

//设置鼠标移动事件
var SetControlMouseMove = function (c) {
    if (c.attr("controlType") != "div" && c.attr("controlType") != "panel") {
        c.mousemove(function () {
            c.css("border-right", "1px dashed");
            c.css("cursor", "move");
            //textarea
            if (c.find("input,textarea").attr("type") == "textarea") {
                c.css("border-bottom", "1px dashed");
            }
        }).mouseout(function () {
            c.css("border-right", "0px dashed");
            c.css("border-bottom", "0px dashed");
        });
    }

    //对table特殊处理
    if (c.attr("controlType") == "table") {
        c.mousemove(function () {
            c.find(".table-box").find(".delete-table").removeClass("table-box-show");
        }).mouseout(function () {
            c.find(".table-box").find(".delete-table").addClass("table-box-show");
        });
    }
}
var CancelControlEvent = function () {
    $(".demo").undelegate("[controlType]", "click");
    $(".demo").undelegate("[controlType]", "dblclick");
    $(".demo").find("[controlType]").unbind("mousemove").unbind("mouseout");

    $(".demo").find(".control-selected").removeClass("control-selected");

}

//生成控件
var SetControlCommonAttr = function (parHtml, ret) {
    //界面生成并显示控件
    var ctrlObj = parHtml.find("[controlType]");
    ctrlObj.attr("id", ret.EName);
    ctrlObj.find("input").attr("id", "INPUT" + ret.EName)  //目前只设置了input 控件的ID
    ctrlObj.attr("ConfigInfo", $.toJSON(ret));
    ctrlObj.attr("EventConfig", "{}");
    ctrlObj.css("width", "200px");
    ctrlObj.css("height", "40px");
    ctrlObj.css("position", "absolute");
    var theEvent = window.event || arguments.callee.caller.arguments[0];
    if (theEvent.pageX == null) {  //设置一个默认值
        ctrlObj.css("left", "200px");
        ctrlObj.css("top", "55px");
    } else {
        ctrlObj.css("left", (theEvent.pageX - 380).toString() + "px");
        ctrlObj.css("top", (theEvent.pageY - 65).toString() + "px");
    }
    switch (ctrlObj.attr("controlType")) {
        case "label":
            {
                //对label的高度记性特殊处理
                ctrlObj.css("height", "20px");
                var label = ctrlObj.find("label");
                label.css("font-weight", "normal");
                label.css("font-size", "13px");
                label.css("color", "#666");
                break;
            }
        case "number":
            {
                //var fullWidth = ctrlObj.css("width").replace("px", "");
                //ctrlObj.css("width", fullWidth + "px");
                //ctrlObj.find("input").css("width", fullWidth + "px");
                break;
            }
        case "div":
            {
                //由于div是容器，对其高度和droppable事件进行了特殊处理
                ctrlObj.css("width", "300px");
                ctrlObj.css("height", "200px");
                ctrlObj.droppable({
                    greedy: true,
                    drop: function (event, ui) {
                        $(this).attr("IsContainer", true);
                    },
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "panel":
            {
                //由于panel是容器，对其高度和droppable事件进行了特殊处理
                ctrlObj.css("width", "300px");
                ctrlObj.css("height", "200px");
                ctrlObj.droppable({
                    greedy: true,
                    drop: function (event, ui) {
                        $(this).attr("IsContainer", true);
                    },
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "tabpanel":
            {
                //由于tabpanel是容器，对其高度和droppable事件进行了特殊处理
                ctrlObj.css("width", "300px");
                ctrlObj.css("height", "200px");
                ctrlObj.find(".tab-content").css("height", (ctrlObj.css("height").replace("px", "") - 45) + "px");
                ctrlObj.droppable({
                    greedy: true,
                    drop: function (event, ui) {
                        $(this).attr("IsContainer", true);
                    },
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                //由于tabpanel内有多个页签，每个页签通过id关联，需要动态生成这些id以及关联方式
                ctrlObj.find("a[data-toggle='tab']").each(function (i) {
                    var oldHref = $(this).attr("href");
                    var newHref = ret.EName + "A" + i.toString();
                    ctrlObj.find(oldHref).attr("id", newHref);
                    $(this).attr("href", "#" + newHref);
                });
                ret.ActivePage = ctrlObj.find(".tab-pane.active").attr("id");
                ctrlObj.attr("ConfigInfo", $.toJSON(ret));
                break;
            }
        case "img":
            {
                ctrlObj.css({ "width": "100px", "height": "100px" });
                break;
            }
        case "date":
            {
                var fullWidth = ctrlObj.css("width").replace("px", "");
                ctrlObj.css("width", (parseInt(fullWidth) + 50) + "px");
                ctrlObj.find("input").css("width", fullWidth + "px");
                //ctrlObj.find("input").A("id", ret.EName);
                break;
            }
        case "dictionary":
            {
                var fullWidth = ctrlObj.css("width").replace("px", "");
                ctrlObj.css("width", fullWidth + "px");

                ctrlObj.find("select").multipleSelect({
                    placeholder: "请选择",
                    single: true, //默认单选
                    filter: false,  //默认不显示查询 
                    width: '100%'
                });
                ctrlObj.find("select").multipleSelect("disable"); //在编辑状态禁用
                
                break;
            }
        case "table":
            {
                var fullWidth = ctrlObj.css("width").replace("px", "");
                ctrlObj.css("width", fullWidth + "px");
                //ctrlObj.find(".table-box").css("width", fullWidth + "px");  
                var curDivH = ctrlObj.find("table")[0].offsetHeight + 3;  //修改表单外面div的height
                ctrlObj.height(curDivH);
                break;
            }
        case "line":
            {
                ctrlObj.css("width", "600px");
                break;
            }
        case "radio":
            {
                ctrlObj.css("width", "");
                ctrlObj.css("height", "auto");
                break;
            }
        case "checkbox":
            {
                ctrlObj.css("width", "");
                ctrlObj.css("height", "auto");
                break;
            }
        case "shape":
            {
                ctrlObj.css({ "width": "170px", "height": "170px" });
                break;
            }
    }

    var curContainer = $("#mainContent").find("[IsContainer=true]");
    if (curContainer.length > 0) {
        //计算相对的Left和top
        var cPos = GetInnerCPos(curContainer);
        var cLeft = cPos.split(",")[0];
        var cTop = cPos.split(",")[1];
        var oLeft = parseInt(ctrlObj.css("left").replace("px", "")) - parseInt(cLeft);
        var oTop = parseInt(ctrlObj.css("top").replace("px", "")) - parseInt(cTop);

        ctrlObj.css("left", oLeft.toString() + "px");
        ctrlObj.css("top", oTop.toString() + "px");

        if (curContainer.attr("controlType") == "panel") {
            curContainer.find(".panel-body").append(ctrlObj);
        } else if (curContainer.attr("controlType") == "tabpanel") {
            curContainer.find(".tab-pane.active").append(ctrlObj);
        } else {
            curContainer.append(ctrlObj);
        }

    } else {
        curContainer = $("#mainContent");
        if (curContainer.attr("controlType") == "panel") {
            curContainer.find(".panel-body").append(ctrlObj);
        } else {
            curContainer.append(ctrlObj);
        }
    }

    $(".demo").undelegate(ctrlObj, "mouseenter mouseleave");
    SetControlMouseMove(ctrlObj);

    InitCurC();
    return ctrlObj;
}
//添加一个控件
var Add = function (parCT, parHtml) {
    var ret = null;
    var curCons = $("[controlType = '" + parCT + "']");
    switch (parCT) {
        case "button":
            {
                var sid = 0;
                if (ButtonArr.length > 0) {
                    var lastControl = ButtonArr[ButtonArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号 主要针对已经保存的控件
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new ButtonProp();
                newControlInfo.EName = "BTN" + sid;
                ButtonArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);
                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "text":
            {
                //debugger;
                var sid = 0;
                if (TextArr.length > 0) {
                    var lastControl = TextArr[TextArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new TextProp();
                newControlInfo.EName = "TXT" + sid;
                newControlInfo.CName = "TXT" + sid;
                TextArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "div":
            {
                //添加控件列表记录
                var sid = 0;
                if (DivArr.length > 0) {
                    var lastControl = DivArr[DivArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new DivProp();
                newControlInfo.EName = "DIV" + sid;
                DivArr.push(newControlInfo);

                ret = newControlInfo;
                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "panel":
            {
                //添加控件列表记录
                var sid = 0;
                if (PanelArr.length > 0) {
                    var lastControl = PanelArr[PanelArr.length - 1];
                    sid = lastControl.EName.substring(5);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new PanelProp();
                newControlInfo.EName = "PANEL" + sid;
                PanelArr.push(newControlInfo);

                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent",
                    resize: function (e, ui) {
                        ui.element.find(".panel-body").css("height", (ui.element.css("height").replace("px", "") - 25) + "px");
                        //var fullHeight = ui.element.find(".tab-content").css("height").replace("px", "");
                        //ui.element.find("input").css("width", (fullWidth - 50) + "px")
                    },
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "tabpanel":
            {
                //添加控件列表记录
                var sid = 0;
                if (TabPanelArr.length > 0) {
                    var lastControl = TabPanelArr[TabPanelArr.length - 1];
                    sid = lastControl.EName.substring(8);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new TabPanelProp();
                newControlInfo.EName = "TABPANEL" + sid;
                TabPanelArr.push(newControlInfo);

                ret = newControlInfo;
                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                //对Tab控件需要做特殊处理
                ctrlObj.resizable({
                    containment: "parent",
                    resize: function (e, ui) {
                        ui.element.find(".tab-content").css("height", (ui.element.css("height").replace("px", "") - 45) + "px");
                        //var fullHeight = ui.element.find(".tab-content").css("height").replace("px", "");
                        //ui.element.find("input").css("width", (fullWidth - 50) + "px")
                    },
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "label":
            {
                var sid = 0;
                if (LabelArr.length > 0) {
                    var lastControl = LabelArr[LabelArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new LabelProp();
                newControlInfo.EName = "LBL" + sid;
                LabelArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "number":
            {
                var sid = 0;
                if (NumberArr.length > 0) {
                    var lastControl = NumberArr[NumberArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new NumberProp();
                newControlInfo.EName = "NUM" + sid;
                newControlInfo.CName = "NUM" + sid;
                NumberArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                break;
            }
        case "date":
            {
                var sid = 0;
                if (DateArr.length > 0) {
                    var lastControl = DateArr[DateArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new DateProp();
                newControlInfo.EName = "DAT" + sid;
                newControlInfo.CName = "DAT" + sid;
                DateArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                //对Date控件需要做特殊处理
                ctrlObj.resizable({
                    containment: "parent",
                    resize: function (e, ui) {
                        var fullWidth = ui.element.css("width").replace("px", "");
                        ui.element.find("input").css("width", (fullWidth - 50) + "px");
                        ui.element.find("input").attr("id", "data" + sid);
                    },
                    handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "time":
            {
                var sid = 0;
                if (TimeArr.length > 0) {
                    var lastControl = TimeArr[TimeArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new TimeProp();
                newControlInfo.EName = "TIM" + sid;
                newControlInfo.CName = "TIM" + sid;
                TimeArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                break;
            }
        case "radio":
            {
                var sid = 0;
                if (RadioArr.length > 0) {
                    var lastControl = RadioArr[RadioArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new RadioProp();
                newControlInfo.EName = "RDI" + sid;
                newControlInfo.CName = "RDI" + sid;
                RadioArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                break;
            }
        case "checkbox":
            {
                var sid = 0;
                if (CheckboxArr.length > 0) {
                    var lastControl = CheckboxArr[CheckboxArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new CheckboxProp();
                newControlInfo.EName = "CHK" + sid;
                newControlInfo.CName = "CHK" + sid;
                CheckboxArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "img":
            {
                //添加控件列表记录
                var sid = 0;
                if (ImgArr.length > 0) {
                    var lastControl = ImgArr[ImgArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new ImgProp();
                newControlInfo.EName = "IMG" + sid;
                ImgArr.push(newControlInfo);

                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "shape":
            {
                //添加控件列表记录
                var sid = 0;
                if (ShapeArr.length > 0) {
                    var lastControl = ShapeArr[ShapeArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new ShapeProp();
                newControlInfo.EName = "SHP" + sid;
                newControlInfo.CName = "SHP" + sid;
                ShapeArr.push(newControlInfo);

                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "select":
            {
                var sid = 0;
                if (SelectArr.length > 0) {
                    var lastControl = SelectArr[SelectArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new SelectProp();
                newControlInfo.EName = "Sel" + sid;
                newControlInfo.CName = "Sel" + sid;
                SelectArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "email":
            {
                var sid = 0;
                if (EmailArr.length > 0) {
                    var lastControl = EmailArr[EmailArr.length - 1];
                    sid = lastControl.EName.substring(2);
                } else {
                    //获取已存在控件最大的ID序号
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new EmailProp();
                newControlInfo.EName = "EM" + sid;
                newControlInfo.CName = "EM" + sid;
                EmailArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "dictionary":
            {
                var sid = 0;
                if (DictionaryArr.length > 0) {
                    var lastControl = DictionaryArr[DictionaryArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号,主要针对已经保存的控件
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new DictionaryProp();
                newControlInfo.EName = "DIC" + sid;
                DictionaryArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                //对Dictionary控件需要做特殊处理
                ctrlObj.resizable({
                    containment: "parent",
                    resize: function (e, ui) {
                        //var fullWidth = ui.element.css("width").replace("px", "");
                    },
                    handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
        case "table":
            {
                var sid = 0;
                if (TableArr.length > 0) {
                    var lastControl = TableArr[TableArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号,主要针对已经保存的控件
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new TableProp();
                newControlInfo.EName = "TBL" + sid;
                TableArr.push(newControlInfo);
                ret = newControlInfo;

                var ctrlObj = SetControlCommonAttr(parHtml, ret);

                ctrlObj.resizable({
                    containment: "parent",
                    resize: function (e, ui) {

                    },
                    handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });

                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }

        case "line":
            {
                var sid = 0;
                if (LineArr.length > 0) {
                    var lastControl = LineArr[LineArr.length - 1];
                    sid = lastControl.EName.substring(3);
                } else {
                    //获取已存在控件最大的ID序号,主要针对已经保存的控件
                    for (var i = 0; i < curCons.length; i++) {
                        var maxSid = parseInt(curCons[i].id.substring(3));
                        if (maxSid > sid) {
                            sid = maxSid;
                        }
                    }
                }
                sid++;
                var newControlInfo = new LineProp();
                newControlInfo.EName = "LIN" + sid;
                LineArr.push(newControlInfo);
                ret = newControlInfo;

                //界面生成并显示控件
                var ctrlObj = SetControlCommonAttr(parHtml, ret);
                ctrlObj.draggable({
                    containment: "parent",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                ctrlObj.resizable({
                    containment: "parent", handles: "e",
                    stop: function (e, t) {
                        SaveTemporHtml();
                    }
                });
                break;
            }
    }
    return ret;
}

var btnClickEventConfig = function (o) {
    //连接中加了一个随机数字，是为了解决iframe的缓存问题 shebin amend 2016-4-19
    var ret = "<div style='z-index:9999999'><iframe id='eventFrame' name='eventFrame' style='height:100%; visibility: inherit; width: 100%;z-index: -1;' scrolling='no' frameborder='0' src='dj/ControlPropDialog/Event.htm?EventC=" + o.prop("id") + "'></iframe></div>";
    $(ret).dialog({
        modal: true,             // 创建模式对话框
        title: "事件配置 - " + o.parent().parent().parent().parent().find("#txtEName").val(),
        width: 1100,
        height: 580,
        buttons: {
            "保存": function () {
                //表达式的取值前面必须加$(this)进行过滤，否则会取到之前缓存的内容 shebin amend 2016-4-19
                var curExpress = $(this).find("#eventFrame")[0].contentWindow.GetFinalExpress();
                o.val(curExpress);
                $(this).dialog("close");
            },
            "关闭": function () { $(this).dialog("close"); }
        }
    });
}

//显示控件的相关的属性
var ShowProp = function (parCtrl, parDialog) {
    switch (parCtrl.attr("controlType")) {
        case "div":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#selClass").val(ctrlInfo.Class);
                break;
            }
        case "panel":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(unescape(ctrlInfo.EName));
                if (ctrlInfo.CName == null) {
                    $(parDialog).find("#txtCName").val(parCtrl[0].innerText);
                }
                else
                    $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#selClass").val(unescape(ctrlInfo.Class));
                break;
            }
        case "tabpanel":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(unescape(ctrlInfo.EName));
                var optionStr = "";
                parCtrl.find("a[data-toggle='tab']").each(function (i) {
                    var val = $(this).attr("href").replace("#", "");
                    optionStr += "<option value='" + val + "'>" + $(this).text() + "</option>";
                });
                $(parDialog).find("#selActivePage").html(optionStr);
                $(parDialog).find("#selActivePage").val(ctrlInfo.ActivePage);
                $(parDialog).find("#selActivePage1").html(optionStr);
                $(parDialog).find("#selActivePage1").val(ctrlInfo.ActivePage);
                //切换到当前页签的时候的事件
                $(parDialog).find("#selActivePage1").change(function () {
                    $(parDialog).find("#txtCName").val($(this).find("option:selected").text());
                    //查找当前页签下的所有字段
                    var curTabID = $(parDialog).find("#selActivePage1").val();
                    var curTabControls = $("#" + curTabID).find("[controlType='text'],[controlType='number'],[controlType='date'],[controlType='time'],[controlType='email'],[controlType='dictionary']");
                    var innerStr = "";
                    curTabControls.each(function (i) {
                        var tmpControl = curTabControls[i];
                        var ctrolInfo = eval("(" + $(tmpControl).attr("ConfigInfo") + ")");
                        innerStr += "<tr><td><input type='checkbox' /></td>";
                        innerStr += "<td style='word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;' title='" + ctrolInfo.CName + "'>" + ctrolInfo.CName + "</td>";
                        innerStr += "<td>" + ctrolInfo.Code + "</td>";
                        innerStr += "<td>" + $(tmpControl).attr("controltype") + "</td>";
                        innerStr += "<td><a id='" + i + "' class='moveto' href='javascript:;'>移动到...</a></td><tr>";
                    });
                    $(parDialog).find("#tbTabFieldList").find("tr:gt(0)").remove();
                    $(parDialog).find("#tbTabFieldList").append(innerStr);
                    var reTabID = "";
                    var getContentItems = function () {
                        var ret = "<select id='selActivePage2'><option></option>";
                        //var val = $(this).attr("href").replace("#", "");
                        $(parDialog).find("#selActivePage1").find("option").each(function (i) {
                            if ($(this).text() != $(parDialog).find("#selActivePage1").find("option:selected").text()) {
                                ret += "<option value='" + $(this).val() + "'>" + $(this).text() + "</option>";
                            }
                        });
                        ret += "</select>";
                        if (ret == "<select id='selActivePage2'><option></option></select>") {
                            $(".moveto").popover('hide');
                        }
                        return ret;
                    }
                    //定义【移动到...】的popover事件
                    $(".moveto").popover({
                        title: "请选择页签",
                        html: true,
                        trigger: "click",
                        placement: "left",
                        content: getContentItems()
                    }).on('shown.bs.popover', function () {
                        var curMovetoID = $(this).attr("id");
                        $("#selActivePage2").change(function () {
                            var list = curTabControls.splice(curMovetoID, 1);
                            var strlist = list[0];
                            innerStr = "";
                            $(parDialog).find("#tbTabFieldList").find("tr:gt(0)").remove();
                            curTabControls.each(function (i) {
                                var tmpControl = curTabControls[i];
                                var ctrolInfo = eval("(" + $(tmpControl).attr("ConfigInfo") + ")");
                                innerStr += "<tr><td><input type='checkbox' /></td>";
                                innerStr += "<td style='word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;' title='" + ctrolInfo.CName + "'>" + ctrolInfo.CName + "</td>";
                                innerStr += "<td>" + ctrolInfo.Code + "</td>";
                                innerStr += "<td>" + $(tmpControl).attr("controltype") + "</td>";
                                innerStr += "<td><a id='" + i + "' class='moveto' href='javascript:;'>移动到...</a></td><tr>";
                            });
                            $(parDialog).find("#tbTabFieldList").append(innerStr);
                            $(strlist).remove();
                            var reTabID = $(this).val();
                            parCtrl.find(".tab-content").find("#" + reTabID + "").append(strlist);
                        });

                    })
                });
                //新增一个Tab
                $(parDialog).find("#btnAddTab").click(function () {
                    var tabTemplate = "<li><a href='#{href}' data-toggle='tab'>#{label}</a></li>";
                    var tabContent = "<div class='tab-pane' style='height: 100%;' id='#{id}'></div>";

                    var newTabName = $(parDialog).find("#txtCName").val();
                    var isExists = false;
                    $(parDialog).find("#selActivePage1").find("option").each(function (i) {
                        if ($(this).text() == newTabName) {
                            DialogOKSimple("[" + newTabName + "] 已经存在,不能重复！");
                            isExists = true;
                        }
                    });
                    if (!isExists) {
                        var oldTabVal = $(parDialog).find("#selActivePage").find("option:last").val();
                        var tabPanelID = "TABPANEL" + oldTabVal.replace("TABPANEL", "").split("A")[0];
                        var tabPanelNO = parseInt(oldTabVal.replace("TABPANEL", "").split("A")[1]) + 1;
                        var newTabVal = tabPanelID + "A" + tabPanelNO;
                        var newItem = "<option value='" + newTabVal + "'>" + newTabName + "</option>";
                        //在页面上更新显示
                        var label = newTabName;
                        var id = newTabVal;
                        var li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label));
                        var tabContentHtml = $(tabContent.replace(/#\{id\}/g, id));

                        parCtrl.find(".nav.nav-tabs").append(li);
                        parCtrl.find(".tab-content").append(tabContentHtml);
                        //在下拉列表中添加新纪录
                        $(parDialog).find("#selActivePage").append(newItem);
                        $(parDialog).find("#selActivePage1").append(newItem);
                        $(parDialog).find("#selActivePage1").val(newTabVal);
                        $(parDialog).find("#selActivePage1").change();
                    }
                    return false;
                });
                //修改一个Tab
                $(parDialog).find("#btnUpdateTab").click(function () {
                    var newTabName = $(parDialog).find("#txtCName").val();
                    var curTabVal = $(parDialog).find("#selActivePage1").val();
                    var isExists = false;
                    //修改的Tab名字不能跟其他的Tab名字重复
                    $(parDialog).find("#selActivePage1").find("option").each(function (i) {
                        if ($(this).text() == newTabName && $(this).val() != curTabVal) {
                            DialogOKSimple("[" + newTabName + "] 已经存在,不能重复！");
                            isExists = true;
                        }
                    });
                    if (!isExists) {
                        parCtrl.find("a[href = '#" + curTabVal + "']").text(unescape(newTabName));
                        $(parDialog).find("#selActivePage1").find("option[value='" + curTabVal + "']").text(newTabName);
                        $(parDialog).find("#selActivePage").find("option[value='" + curTabVal + "']").text(newTabName);
                    }
                    return false;
                });
                $(parDialog).find("#btnDelTab").click(function () {
                    if ($(parDialog).find("#selActivePage1").find("option").length == 1) {
                        DialogOKSimple("必须保留一个页签！");
                        return false;
                    } else if ($(parDialog).find("#tbTabFieldList").find("tr").length > 1) {
                        DialogOKSimple("页签中含有字段，不能删除！");
                        return false;
                    }
                    var curTabVal = $(parDialog).find("#selActivePage1").val();
                    parCtrl.find("ul li a[href = '#" + curTabVal + "']").parent().remove();
                    parCtrl.find("div[id='" + curTabVal + "']").remove();
                    $(parDialog).find("#selActivePage1").find("option[value='" + curTabVal + "']").remove();
                    $(parDialog).find("#selActivePage").find("option[value='" + curTabVal + "']").remove();
                    $(parDialog).find("#selActivePage1").change();
                    return false;
                });
                $(parDialog).find("#selActivePage1").change();
                break;
            }
        case "label":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(unescape(ctrlInfo.EName));
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtInnerText").val(unescape(parCtrl[0].innerText));
                break;
            }
        case "button":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.Text));
                $(parDialog).find("#selClass option[value='" + ctrlInfo.Class + "']").prop("selected", true);
                $(parDialog).find("#selSize option[value='" + ctrlInfo.Size + "']").prop("selected", true);

                break;
            }
        case "img":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#selClass").val(ctrlInfo.Class); //下拉选择后保存当前选择项
                if (ctrlInfo.Url == "") {
                    var srcUrl = $("img").attr("src");
                    $(parDialog).find("#txtUrl").val(srcUrl);
                }
                else
                    $(parDialog).find("#txtUrl").val(ctrlInfo.Url);

                break;
            }
        case "text":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var ctrlEventInfo = eval("(" + parCtrl.attr("EventConfig") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                if (ctrlInfo.SelType != undefined && ctrlInfo.SelType != null && ctrlInfo.SelType != "") {
                    $(parDialog).find("#selType").val(ctrlInfo.SelType);
                }
                ShowColor(parDialog, ctrlInfo);

                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }
                

                //还原事件
                //$(parDialog).find("#txtClickEvent").val(ctrlInfo.EventClick);
                //$(parDialog).find("#txtDBLClickEvent").val(ctrlInfo.EventDBLClick);
                //$(parDialog).find("#txtEnterEvent").val(ctrlInfo.EventEnter);
                //$(parDialog).find("#txtBlurEvent").val(ctrlInfo.EventBlur);
                //$(parDialog).find("#txtKeyUpEvent").val(ctrlInfo.EventKeyUp);
                //加判断，考虑无事件信息情况  add by chengl
                if (ctrlEventInfo) {
                    $(parDialog).find("#txtClickEvent").val(ctrlEventInfo.EventClick);
                    $(parDialog).find("#txtDBLClickEvent").val(ctrlEventInfo.EventDBLClick);
                    $(parDialog).find("#txtEnterEvent").val(ctrlEventInfo.EventEnter);
                    $(parDialog).find("#txtBlurEvent").val(ctrlEventInfo.EventBlur);
                    $(parDialog).find("#txtKeyUpEvent").val(ctrlEventInfo.EventKeyUp);
                }
                break;
            }
        case "number":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var ctrlEventInfo = eval("(" + parCtrl.attr("EventConfig") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                //还原事件
                //$(parDialog).find("#txtClickEvent").val(ctrlInfo.EventClick);
                //$(parDialog).find("#txtDBLClickEvent").val(ctrlInfo.EventDBLClick);
                //$(parDialog).find("#txtEnterEvent").val(ctrlInfo.EventEnter);
                //$(parDialog).find("#txtBlurEvent").val(ctrlInfo.EventBlur);
                //$(parDialog).find("#txtKeyUpEvent").val(ctrlInfo.EventKeyUp);
                //加判断，考虑无事件信息情况  add by chengl
                if (ctrlEventInfo) {
                    $(parDialog).find("#txtClickEvent").val(ctrlEventInfo.EventClick);
                    $(parDialog).find("#txtDBLClickEvent").val(ctrlEventInfo.EventDBLClick);
                    $(parDialog).find("#txtEnterEvent").val(ctrlEventInfo.EventEnter);
                    $(parDialog).find("#txtBlurEvent").val(ctrlEventInfo.EventBlur);
                    $(parDialog).find("#txtKeyUpEvent").val(ctrlEventInfo.EventKeyUp);
                }

                break;
            }
        case "date":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var ctrlEventInfo = eval("(" + parCtrl.attr("EventConfig") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                //还原事件
                //$(parDialog).find("#txtClickEvent").val(ctrlInfo.EventClick);
                //$(parDialog).find("#txtDBLClickEvent").val(ctrlInfo.EventDBLClick);
                //$(parDialog).find("#txtEnterEvent").val(ctrlInfo.EventEnter);
                //$(parDialog).find("#txtBlurEvent").val(ctrlInfo.EventBlur);
                //$(parDialog).find("#txtKeyUpEvent").val(ctrlInfo.EventKeyUp);
                //加判断，考虑无事件信息情况  add by chengl
                if (ctrlEventInfo) {
                    $(parDialog).find("#txtClickEvent").val(ctrlEventInfo.EventClick);
                    $(parDialog).find("#txtDBLClickEvent").val(ctrlEventInfo.EventDBLClick);
                    $(parDialog).find("#txtEnterEvent").val(ctrlEventInfo.EventEnter);
                    $(parDialog).find("#txtBlurEvent").val(ctrlEventInfo.EventBlur);
                    $(parDialog).find("#txtKeyUpEvent").val(ctrlEventInfo.EventKeyUp);
                }
                break;
            }
        case "time":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                break;
            }
        case "radio":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                if (ctrlInfo.SName == null) {
                    //显示默认值，先简单直接赋值给textarea
                    $(parDialog).find("#txtSName").val("选项1" + "\n" + "选项2" + "\n");
                }
                else
                    $(parDialog).find("#txtSName").val(ctrlInfo.SName);
                if (ctrlInfo.Value == null) {
                    //显示默认值，先简单直接赋值给textarea
                    $(parDialog).find("#txtValue").val("1\n2\n");
                }
                else
                    $(parDialog).find("#txtValue").val(ctrlInfo.Value);
                //赋值给显示字段和取值字段的select
                var optionStrName = "";
                var optionStrVal = ""
                parCtrl.find("input[type='radio']").each(function (i) {
                    //var val = $(this).attr("value");
                    optionStrName += "<option>" + $(this).parent().text() + "</option>";
                    optionStrVal += "<option>" + $(this).val() + "</option>";
                });
                $(parDialog).find("#nameSelField").html(optionStrName);
                $(parDialog).find("#valueSelField").html(optionStrVal);

                if (ctrlInfo.HVstyle == "2") {
                    $(parDialog).find("#Hstyle").attr("selected", "selected");
                }
                else {
                    $(parDialog).find("#Vstyle").attr("selected", "selected");
                }
                break;
            }
        case "checkbox":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                if (ctrlInfo.SName == null) {
                    $(parDialog).find("#txtSName").val(parCtrl[0].innerText + "\n");
                }
                else
                    $(parDialog).find("#txtSName").val(ctrlInfo.SName);
                if (ctrlInfo.Value == null) {
                    //显示默认值，先简单直接赋值给textarea
                    $(parDialog).find("#txtValue").val("1\n");
                }
                else
                    $(parDialog).find("#txtValue").val(ctrlInfo.Value);
                //赋值给显示字段和取值字段的select
                var optionStrName = "";
                var optionStrVal = ""
                parCtrl.find("input[type='checkbox']").each(function (i) {
                    //var val = $(this).attr("value");
                    optionStrName += "<option>" + $(this).parent().text() + "</option>";
                    optionStrVal += "<option>" + $(this).val() + "</option>";
                });
                $(parDialog).find("#nameSelField").html(optionStrName);
                $(parDialog).find("#valueSelField").html(optionStrVal);

                if (ctrlInfo.HVstyle == "2") {
                    $(parDialog).find("#Hstyle").attr("selected", "selected");
                }
                else {
                    $(parDialog).find("#Vstyle").attr("selected", "selected");
                }
                break;
            }
        case "select":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                break;
            }
        case "email":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                break;
            }
        case "dictionary":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#txtDataFieldName").val(ctrlInfo.DataFieldName);
                $(parDialog).find("#radioSingleSelect").val(ctrlInfo.SingleSelect);
                $(parDialog).find("#selDicTableName").val(unescape(ctrlInfo.DicTableName));
                $(parDialog).find("#radioFontFamily").val(ctrlInfo.FontFamily);
                $(parDialog).find("#numFontSize").val(ctrlInfo.FontSize);
                $(parDialog).find("#radioFontStyle").val(ctrlInfo.FontStyle);
                ShowColor(parDialog, ctrlInfo);
                //是否必填，回显
                var isRequired = parCtrl.attr("isRequired");
                if (isRequired) {
                    $(parDialog).find("#radioRequired").val(isRequired);
                } else {
                    $(parDialog).find("#radioRequired").val(0);
                }

                break;
            }
        case "table":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#row").val(ctrlInfo.row);
                $(parDialog).find("#col").val(ctrlInfo.col);
                break;
            }
        case "line":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                break;
            }
        case "shape":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                $(parDialog).find("#txtEName").val(ctrlInfo.EName);
                $(parDialog).find("#txtCName").val(unescape(ctrlInfo.CName));
                $(parDialog).find("#txtCode").val(ctrlInfo.Code);
                $(parDialog).find("#LineWidth").val(ctrlInfo.LWidth);
                $(parDialog).find("#LineColor").val(ctrlInfo.LineColor);
                $(parDialog).find("#ShapeColor").val(ctrlInfo.ShapeColor);
                $(parDialog).find("#LineColor").spectrum({
                    showPaletteOnly: true,
                    togglePaletteOnly: true,
                    togglePaletteMoreText: 'more',
                    togglePaletteLessText: 'less',
                    color: ctrlInfo.LColor,
                    palette: [
                        ["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
                        ["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
                        ["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
                        ["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
                        ["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
                        ["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
                        ["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
                        ["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
                    ]
                });
                $(parDialog).find("#ShapeColor").spectrum({
                    showPaletteOnly: true,
                    togglePaletteOnly: true,
                    togglePaletteMoreText: 'more',
                    togglePaletteLessText: 'less',
                    color: ctrlInfo.SColor,
                    palette: [
                        ["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
                        ["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
                        ["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
                        ["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
                        ["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
                        ["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
                        ["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
                        ["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
                    ]
                });

                switch (ctrlInfo.Sstyle) {
                    case "1":   //圆形
                        {
                            $(parDialog).find("#Circle").attr("selected", "selected");
                            break;
                        }
                    case "2":   //圆角矩形
                        {
                            $(parDialog).find("#CRect").attr("selected", "selected");
                            break;
                        }
                    case "3":   //尖角矩形
                        {
                            $(parDialog).find("#SRect").attr("selected", "selected");
                            break;
                        }
                    case "4":   //右下箭头
                        {
                            $(parDialog).find("#RDLineMarker").attr("selected", "selected");
                            break;
                        }
                    case "5":   //右上箭头
                        {
                            $(parDialog).find("#RULineMarker").attr("selected", "selected");
                            break;
                        }
                    case "6":   //上箭头
                        {
                            $(parDialog).find("#ULineMarker").attr("selected", "selected");
                            break;
                        }
                    case "7":   //下箭头
                        {
                            $(parDialog).find("#DLineMarker").attr("selected", "selected");
                            break;
                        }
                    case "8":   //左箭头
                        {
                            $(parDialog).find("#LLineMarker").attr("selected", "selected");
                            break;
                        }
                    case "9":   //右箭头
                        {
                            $(parDialog).find("#RLineMarker").attr("selected", "selected");
                            break;
                        }
                    case "10":   //横线
                        {
                            $(parDialog).find("#HLine").attr("selected", "selected");
                            break;
                        }
                    case "11":   //竖线
                        {
                            $(parDialog).find("#VLine").attr("selected", "selected");
                            break;
                        }
                    case "12":   //左斜线
                        {
                            $(parDialog).find("#LLine").attr("selected", "selected");
                            break;
                        }
                    case "13":   //右斜线
                        {
                            $(parDialog).find("#RLine").attr("selected", "selected");
                            break;
                        }
                }
                var styleTxt = $("#radioSingleSelect").find("option:selected").text();
                $(parDialog).find("#shapeStyle").val(styleTxt);
                break;
            }
    }
    //绑定配置事件按钮的事件
    $(parDialog).find("#eventProp").find("button").click(function () { btnClickEventConfig($(this).parent().find("input")); });
}

//保存控件相关的属性
var SaveProp = function (parCtrl, parDialog) {
    switch (parCtrl.attr("controlType")) {
        case "div":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = escape($(parDialog).find("#txtEName").val());
                ctrlInfo.Class = escape($(parDialog).find("#selClass").val());
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));

                //设置界面
                parCtrl.removeClass();
                parCtrl.addClass(unescape(ctrlInfo.Class));

                break;
            }
        case "panel":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                //ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Class = escape($(parDialog).find("#selClass").val());
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                //设置界面
                parCtrl.find(".panel-heading").text(unescape(ctrlInfo.CName));
                parCtrl.removeClass();
                parCtrl.addClass(unescape(ctrlInfo.Class));
                break;

            }
        case "label":
            {
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                //设置界面
                parCtrl.find("label").text(unescape($(parDialog).find("#txtInnerText").val()));
                break;
            }
        case "button":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.Text = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Class = $(parDialog).find("#selClass").val();
                ctrlInfo.Size = $(parDialog).find("#selSize").val();

                //设置界面
                parCtrl.find("button").text(unescape(ctrlInfo.Text));
                parCtrl.find("button").removeClass();
                parCtrl.find("button").addClass(ctrlInfo.Class);
                if (ctrlInfo.Size != "btn-default") {
                    parCtrl.find("button").addClass(ctrlInfo.Size);
                }
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                break;
            }
        case "img":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.Url = escape($(parDialog).find("#txtUrl").val());
                ctrlInfo.Class = $(parDialog).find("#selClass").val();

                //设置界面
                parCtrl.find("img").attr("src", unescape(ctrlInfo.Url)); //显示默认图片路径
                parCtrl.find("img").removeClass();
                parCtrl.find("img").addClass(ctrlInfo.Class);
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                break;
            }
        case "text":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.SelType = $(parDialog).find("#selTextType").val();
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //绑定事件
                //ctrlInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                //ctrlInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                //ctrlInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                //ctrlInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                //ctrlInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();

                var ctrlEventInfo = new Object();
                ctrlEventInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                ctrlEventInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                ctrlEventInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                ctrlEventInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                ctrlEventInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("EventConfig", $.toJSON(ctrlEventInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);

                //更改控件类型
                ChangeInputType(parCtrl, ctrlInfo.SelType);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor ) 
                {
                    SaveTemporHtml();
                }
                break;
            }
        case "number":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //绑定事件
                //ctrlInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                //ctrlInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                //ctrlInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                //ctrlInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                //ctrlInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();

                var ctrlEventInfo = new Object();
                ctrlEventInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                ctrlEventInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                ctrlEventInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                ctrlEventInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                ctrlEventInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("EventConfig", $.toJSON(ctrlEventInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "date":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //绑定事件
                //ctrlInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                //ctrlInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                //ctrlInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                //ctrlInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                //ctrlInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();
                
                var ctrlEventInfo = new Object();
                ctrlEventInfo.EventClick = $(parDialog).find("#txtClickEvent").val();
                ctrlEventInfo.EventDBLClick = $(parDialog).find("#txtDBLClickEvent").val();
                ctrlEventInfo.EventEnter = $(parDialog).find("#txtEnterEvent").val();
                ctrlEventInfo.EventBlur = $(parDialog).find("#txtBlurEvent").val();
                ctrlEventInfo.EventKeyUp = $(parDialog).find("#txtKeyUpEvent").val();

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("EventConfig", $.toJSON(ctrlEventInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "time":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "radio":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.HVstyle = $(parDialog).find("#radioSingleSelect").val(); //单选样式取值

                //保存textarea里面值前先做判断
                var curSName = $(parDialog).find("#txtSName").val();
                var curValue = $(parDialog).find("#txtValue").val();
                if (curSName == "" || curValue == "") {
                    alert("取值或显示不能为空。");
                    return;
                }
                else if (curSName.trim().split("\n").length == 1) {
                    alert("单选项不能为一个。");
                    return;
                }
                else if (curSName.trim().split("\n").length != curValue.trim().split("\n").length) {
                    alert("取值与显示未一一对应。");
                    return;
                }
                else {
                    //添加判断，取值不能相同
                    var ary = curValue.trim().split("\n");
                    var nary = ary.sort();
                    for (var i = 0; i < ary.length; i++) {
                        if (nary[i] == nary[i + 1]) {
                            alert("取值存在重复：" + nary[i]);
                            return;
                        }
                    }
                    ctrlInfo.SName = $(parDialog).find("#txtSName").val() + "\n";
                    ctrlInfo.Value = $(parDialog).find("#txtValue").val() + "\n";
                }
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));

                //将更改的显示与取值通过html拼接展示在前台
                var curHtml = "";
                var curSName = $(parDialog).find("#txtSName").val();
                var curValue = $(parDialog).find("#txtValue").val();
                //先判断排版方式再拼接html,排版默认为竖版排列HVstyle=1
                if (ctrlInfo.HVstyle == "2") {
                    for (i = 0; i < curSName.trim().split("\n").length; i++) {
                        curHtml = curHtml + "<div style='float:left;margin-right:10px;'><input type='radio' name='radioDj' value='" +
                            curValue.trim().split("\n")[i] + "'/>" + curSName.trim().split("\n")[i] + "</div>";
                    }
                }
                else {
                    for (i = 0; i < curSName.trim().split("\n").length; i++) {
                        curHtml = curHtml + "<div><input type='radio' name='radioDj' value='" +
                            curValue.trim().split("\n")[i] + "'/>" + curSName.trim().split("\n")[i] + "</div>";
                    }
                }
                var curID = parCtrl.attr('id');
                $("#" + curID).html("");
                $("#" + curID).append(curHtml);
                break;
            }
        case "checkbox":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.HVstyle = $(parDialog).find("#radioSingleSelect").val(); //多选样式取值
                //ctrlInfo.SName = $(parDialog).find("#txtSName").val();

                var curSName = $(parDialog).find("#txtSName").val();
                var curValue = $(parDialog).find("#txtValue").val();
                if (curSName == "" || curValue == "") {
                    alert("取值或显示不能为空。");
                    return;
                }
                else if (curSName.trim().split("\n").length != curValue.trim().split("\n").length) {
                    alert("取值与显示未一一对应。");
                    return;
                }
                else {
                    //添加判断，取值不能相同
                    var ary = curValue.trim().split("\n");
                    var nary = ary.sort();
                    for (var i = 0; i < ary.length; i++) {
                        if (nary[i] == nary[i + 1]) {
                            alert("取值存在重复：" + nary[i]);
                            return;
                        }
                    }
                    ctrlInfo.SName = $(parDialog).find("#txtSName").val() + "\n";
                    ctrlInfo.Value = $(parDialog).find("#txtValue").val() + "\n"
                }
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                //parCtrl.find("label").html("<label disabled class ='radio-inline' style='width:100%'><input type='checkbox' />"+ctrlInfo.SName+"</label>");
                //更改标签显示时不能用.text()赋值的方式

                //将更改的显示与取值通过html拼接展示在前台
                var curHtml = "";
                var curSName = $(parDialog).find("#txtSName").val();
                var curValue = $(parDialog).find("#txtValue").val();


                if (ctrlInfo.HVstyle == "2") {
                    for (i = 0; i < curSName.trim().split("\n").length; i++) {
                        curHtml = curHtml + "<label disabled class ='radio-inline' style='float:left;'><input type='checkbox' value='"
                            + curValue.trim().split("\n")[i] + "'/>" + curSName.trim().split("\n")[i] + "</label>";
                    }
                }
                else {
                    //显示为选中状态
                    for (i = 0; i < curSName.trim().split("\n").length; i++) {
                        curHtml = curHtml + "<label disabled class ='radio-inline'><input type='checkbox' value='"
                            + curValue.trim().split("\n")[i] + "'/>" + curSName.trim().split("\n")[i] + "</label><br>";
                    }
                }
                var curID = parCtrl.attr('id');
                $("#" + curID).html("");
                $("#" + curID).append(curHtml);

                break;
            }
        case "select":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "email":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "dictionary":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                var lastLineColor = ctrlInfo.LineColor;
                var lastShapeColor = ctrlInfo.ShapeColor;
                var lastFontColor = ctrlInfo.FontColor;
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.SingleSelect = $(parDialog).find("#radioSingleSelect").val();
                ctrlInfo.DicTableName = $(parDialog).find("#selDicTableName").val();
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                ctrlInfo.FontColor = $(parDialog).find("#FColor .sp-preview-inner").css("background-color");//文字颜色
                ctrlInfo.FontFamily = $(parDialog).find("#radioFontFamily").val();//字体
                ctrlInfo.FontStyle = $(parDialog).find("#radioFontStyle").val();//字形
                ctrlInfo.FontSize = $(parDialog).find("#numFontSize").val();//字号

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());
                SaveColor(parCtrl, ctrlInfo);
                if (lastLineColor != ctrlInfo.LineColor || lastShapeColor !== ctrlInfo.ShapeColor) {
                    SaveTemporHtml();
                }
                break;
            }
        case "table":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.row = escape($(parDialog).find("#row").val());
                ctrlInfo.col = $(parDialog).find("#col").val();
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                parCtrl.attr("isRequired", $(parDialog).find("#radioRequired").val());

                //根据属性更改表格
                ChangeTable(parCtrl, ctrlInfo);  // ChangeInputType(parCtrl, ctrlInfo.SelType);
                break;
            }
        case "line":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = escape($(parDialog).find("#txtEName").val());
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));
                //设置界面
                parCtrl.find("span").text(unescape(ctrlInfo.CName));

                break;
            }
        case "shape":
            {
                //搜集属性数据
                var ctrlInfo = eval("(" + parCtrl.attr("ConfigInfo") + ")");
                ctrlInfo.EName = $(parDialog).find("#txtEName").val();
                ctrlInfo.CName = escape($(parDialog).find("#txtCName").val());
                ctrlInfo.Code = $(parDialog).find("#txtCode").val();
                ctrlInfo.Sstyle = $(parDialog).find("#radioSingleSelect").val(); //图形样式取值
                ctrlInfo.LineColor = $(parDialog).find("#LColor .sp-preview-inner").css("background-color"); //线条颜色
                ctrlInfo.ShapeColor = $(parDialog).find("#SColor .sp-preview-inner").css("background-color"); //图形颜色
                //箭头不能改变线条宽度
                if (ctrlInfo.Sstyle == "4" || ctrlInfo.Sstyle == "5" || ctrlInfo.Sstyle == "6" || ctrlInfo.Sstyle == "7" || ctrlInfo.Sstyle == "8" || ctrlInfo.Sstyle == "9") {
                    ctrlInfo.LWidth = "1";
                }
                else {
                    ctrlInfo.LWidth = $(parDialog).find("#LineWidth").val();
                }

                //保存数据到控件上
                parCtrl.attr("ConfigInfo", $.toJSON(ctrlInfo));

                //控件样式
                switch (ctrlInfo.Sstyle) {

                    case "1":   //圆形
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><ellipse cx="50%" cy="50%" rx="49%" ry="49%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '" fill="' + ctrlInfo.ShapeColor + '"/></svg>';
                            break;
                        }
                    case "2":   //圆角矩形
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><rect rx="9" ry="9" width="100%" height="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '" fill="' + ctrlInfo.ShapeColor + '"/></svg>';
                            break;
                        }
                    case "3":   //尖角矩形
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><rect width="100%" height="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '" fill="' + ctrlInfo.ShapeColor + '"/></svg>';
                            break;
                        }
                    case "4":   //右下箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill: ' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="0" y1="0" x2="100%" y2="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="1"></line></svg>';
                            break;
                        }
                    case "5":   //右上箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill: ' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="0" y1="100%" x2="100%" y2="0" stroke="' + ctrlInfo.LineColor + '" stroke-width="1" ></line></svg>';
                            break;
                        }
                    case "6":   //上箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill:' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="50%" y1="100%" x2="50%" y2="0" stroke="' + ctrlInfo.LineColor + '" stroke-width="1" ></line></svg>';
                            break;
                        }
                    case "7":   //下箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill: ' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="50%" y1="0" x2="50%" y2="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="1" ></line></svg>';
                            break;
                        }
                    case "8":   //左箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill: ' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="100%" y1="50%" x2="0" y2="50%" stroke="' + ctrlInfo.LineColor + '" stroke-width="1" ></line></svg>';
                            break;
                        }
                    case "9":   //右箭头
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><defs><marker id="arrow" refX="8" refY="0" markerWidth="20" markerHeight="20" orient="auto" viewBox="0, -3, 8, 6" ><path d="M0 -3 L0 3 L8 0" style="fill:' + ctrlInfo.LineColor + ';"></path></marker></defs><line class="link" marker-end="url(#arrow)" x1="0" y1="50%" x2="100%" y2="50%" stroke="' + ctrlInfo.LineColor + '" stroke-width="1" ></line></svg>';
                            break;
                        }
                    case "10":   //横线
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><line x1="0" y1="50%" x2="100%" y2="50%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '"/></svg>';
                            break;
                        }
                    case "11":   //竖线
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><line x1="50%" y1="0" x2="50%" y2="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '"/></svg>';
                            break;
                        }
                    case "12":  //左斜线
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><line x1="0" y1="0" x2="100%" y2="100%" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '"/></svg>';
                            break;
                        }
                    case "13":  //右斜线
                        {
                            curHtml = '<svg id="isvg" width="100%" height="100%"><line x1="0" y1="100%" x2="100%" y2="0" stroke="' + ctrlInfo.LineColor + '" stroke-width="' + ctrlInfo.LWidth + '"/></svg>';
                            break;
                        }
                }

                parCtrl.find("svg").replaceWith(curHtml);
                SaveTemporHtml();
                break;

            }
    }
}
var ShowColor = function (parDialog, ctrlInfo) {
    $(parDialog).find("#LineColor").spectrum({
        showPaletteOnly: true,
        togglePaletteOnly: true,
        togglePaletteMoreText: 'more',
        togglePaletteLessText: 'less',
        color: ctrlInfo.LineColor,
        palette: [
            ["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
            ["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
            ["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
            ["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
            ["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
            ["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
            ["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
            ["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
        ]
    });
    $(parDialog).find("#BackgroundColor").spectrum({
        showPaletteOnly: true,
        togglePaletteOnly: true,
        togglePaletteMoreText: 'more',
        togglePaletteLessText: 'less',
        color: ctrlInfo.ShapeColor,
        palette: [
            ["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
            ["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
            ["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
            ["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
            ["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
            ["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
            ["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
            ["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
        ]
    });
    $(parDialog).find("#FontColor").spectrum({
        showPaletteOnly: true,
        togglePaletteOnly: true,
        togglePaletteMoreText: 'more',
        togglePaletteLessText: 'less',
        color: ctrlInfo.FontColor,
        palette: [
            ["#000", "#444", "#666", "#999", "#ccc", "#eee", "#f3f3f3", "#fff"],
            ["#f00", "#f90", "#ff0", "#0f0", "#0ff", "#00f", "#90f", "#f0f"],
            ["#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3", "#d0e0e3", "#cfe2f3", "#d9d2e9", "#ead1dc"],
            ["#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8", "#a2c4c9", "#9fc5e8", "#b4a7d6", "#d5a6bd"],
            ["#e06666", "#f6b26b", "#ffd966", "#93c47d", "#76a5af", "#6fa8dc", "#8e7cc3", "#c27ba0"],
            ["#c00", "#e69138", "#f1c232", "#6aa84f", "#45818e", "#3d85c6", "#674ea7", "#a64d79"],
            ["#900", "#b45f06", "#bf9000", "#38761d", "#134f5c", "#0b5394", "#351c75", "#741b47"],
            ["#600", "#783f04", "#7f6000", "#274e13", "#0c343d", "#073763", "#20124d", "#4c1130"]
        ]
    });
}
var SaveColor = function (parCtrl, ctrlInfo) {
    switch (parCtrl.attr("controlType")) {
        case "dictionary":
            {
                parCtrl.find("span").css("border", "1px solid " + ctrlInfo.LineColor);
                parCtrl.find("span").css("background-color", ctrlInfo.ShapeColor);
                parCtrl.find("span").css("color", ctrlInfo.FontColor);
                parCtrl.find("span").css("font-family", ctrlInfo.FontFamily);
                parCtrl.find("span").css("font-size", ctrlInfo.FontSize + "px");
                switch (ctrlInfo.FontStyle) {
                    case "1":
                        {
                            parCtrl.find("span").css("font-style", "normal");
                            parCtrl.find("span").css("font-weight", "normal");
                            break;
                        }
                    case "2":
                        {
                            parCtrl.find("span").css("font-style", "normal");
                            parCtrl.find("span").css("font-weight", "bold");
                            break;
                        }
                    case "3":
                        {
                            parCtrl.find("span").css("font-style", "italic");
                            parCtrl.find("span").css("font-weight", "normal");
                            break;
                        }
                    case "4":
                        {
                            parCtrl.find("span").css("font-style", "italic");
                            parCtrl.find("span").css("font-weight", "bold");
                            break;
                        }
                }
                break;
            }
        case "select":
            {
                parCtrl.find("select").css("border", "1px solid " + ctrlInfo.LineColor);
                parCtrl.find("select").css("background-color", ctrlInfo.ShapeColor);
                parCtrl.find("select").css("color", ctrlInfo.FontColor);
                parCtrl.find("select").css("font-family", ctrlInfo.FontFamily);
                parCtrl.find("select").css("font-size", ctrlInfo.FontSize + "px");
                switch (ctrlInfo.FontStyle) {
                    case "1":
                        {
                            parCtrl.find("select").css("font-style", "normal");
                            parCtrl.find("select").css("font-weight", "normal");
                            break;
                        }
                    case "2":
                        {
                            parCtrl.find("select").css("font-style", "normal");
                            parCtrl.find("select").css("font-weight", "bold");
                            break;
                        }
                    case "3":
                        {
                            parCtrl.find("select").css("font-style", "italic");
                            parCtrl.find("select").css("font-weight", "normal");
                            break;
                        }
                    case "4":
                        {
                            parCtrl.find("select").css("font-style", "italic");
                            parCtrl.find("select").css("font-weight", "bold");
                            break;
                        }
                }
                break;
            }
        default:
            {
                parCtrl.find("input").css("border", "1px solid " + ctrlInfo.LineColor);
                parCtrl.find("input").css("background-color", ctrlInfo.ShapeColor);
                parCtrl.find("input").css("color", ctrlInfo.FontColor);
                parCtrl.find("input").css("font-family", ctrlInfo.FontFamily);
                parCtrl.find("input").css("font-size", ctrlInfo.FontSize + "px");
                switch (ctrlInfo.FontStyle) {
                    case "1":
                        {
                            parCtrl.find("input").css("font-style", "normal");
                            parCtrl.find("input").css("font-weight", "normal");
                            break;
                        }
                    case "2":
                        {
                            parCtrl.find("input").css("font-style", "normal");
                            parCtrl.find("input").css("font-weight", "bold");
                            break;
                        }
                    case "3":
                        {
                            parCtrl.find("input").css("font-style", "italic");
                            parCtrl.find("input").css("font-weight", "normal");
                            break;
                        }
                    case "4":
                        {
                            parCtrl.find("input").css("font-style", "italic");
                            parCtrl.find("input").css("font-weight", "bold");
                            break;
                        }
                }
                break;
            }
    }
    
}
//text->textarea 或者 textarea->text
function ChangeInputType(pControl, pNewType) {
    if (pNewType == undefined || pNewType == null || pNewType == "") return;
    if (pControl.find("input,textarea").attr("type") == pNewType) return;

    //先移除控件整体DIV，然后再添加
    var pControlParent = pControl.parent();
    var newControl = pControl.clone();
    pControl.remove();

    //还原textarea或者text的id、class
    var theInputObj = newControl.find("input,textarea");
    var theInputID = theInputObj.attr("id");
    var theInputClass = theInputObj.attr("class");

    if (pNewType == "textarea") {
        newControl.html("<textarea type='textarea' id='" + theInputID + "' class='" + theInputClass + "' style=' resize: none;'/>");
        pControlParent.append(newControl);
        //设置div高度等于控件高度
        newControl.height(newControl.find("textarea").outerHeight() + 1);
        //允许纵向拖动
        newControl.resizable({
            containment: "parent", handles: "e,s", resize: function (e, ui) {
                ui.element.find("textarea").outerHeight(ui.element.height());
            },
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        newControl.draggable({
            containment: "parent",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
    } else {
        newControl.html("<input type='text' id='" + theInputID + "' class='" + theInputClass + "'/>");

        pControlParent.append(newControl);
        //设置div高度等于控件高度 控件和div之间有空白 所以要用outerHeight
        newControl.height(newControl.find("input").outerHeight());
        //不允许纵向拖动
        newControl.resizable({
            containment: "parent", handles: "e",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        newControl.draggable({
            containment: "parent",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
    }
    SetControlMouseMove(newControl);
}

//根据属性设置更改表格的行和列
function ChangeTable(parCtrl, ctrlInfo) {
    var tabObj = parCtrl.find("table");
    var rows = tabObj[0].rows.length;
    var cols = tabObj[0].rows.item(0).cells.length;
    if (parseInt(ctrlInfo.row) > rows) {
        //添加行
        for (var i = rows; i < parseInt(ctrlInfo.row) ; i++) {
            addTableRow(tabObj[0], i);
         }
    } else if (parseInt(ctrlInfo.row) < rows) {
        //删除行
        for(var k=rows;k>parseInt(ctrlInfo.row); k--){ 
            removeTableRow(tabObj[0], k - 1)
        }
    }
    
    if (parseInt(ctrlInfo.col) > cols) {
        for (var m = cols; m < parseInt(ctrlInfo.col) ; m++) {
            //添加一列
            addTableCell(tabObj[0],m);
        }
    } else if (parseInt(ctrlInfo.col) < cols) {
        //删除列
        for (var k = cols; k > parseInt(ctrlInfo.col) ; k--) {
            removeTableCell(tabObj[0], k-1);
        }
    }
    
    //设置div 的height
    var curDivH = parCtrl.find("table")[0].offsetHeight + 3;  //修改表单外面div的height
    parCtrl.height(curDivH);
}

//region   表格控件操作方法   

//指定位置新增一行
function addTableRow(curTabObj, rowIndex) {
    var cols = curTabObj.rows.item(0).cells.length;
    var myNewRow = curTabObj.insertRow(rowIndex);//插入新行
    myNewRow.style.height = "35px";
    for (var j = 0; j < cols ; j++) {
        var newTdObj = myNewRow.insertCell(j);
        //newTdObj.innerHTML = (rowIndex + 1) + "行" + (j + 1) + "列";
    }
}

//删除一行
function removeTableRow(curTabObj, rowIndex) {
    curTabObj.deleteRow(rowIndex);
}

//添加列
function addTableCell(curTabObj, cellIndex) {
    var rows = curTabObj.rows.length;
    var cols = curTabObj.rows.item(0).cells.length;
    for (var r = 0; r < rows; r++) {
        var cell = curTabObj.rows[r].insertCell(cellIndex);
        //cell.innerHTML = (r + 1) + "行" + (cellIndex+1) + "列";
    }
}

//删除列
function removeTableCell(curTabObj, cellIndex){
    var rows = curTabObj.rows.length;
    var cols = curTabObj.rows.item(0).cells.length;
    for (var r = 0; r < rows; r++) {
        var cell = curTabObj.rows[r].deleteCell(cellIndex);
    }
}
//endregion 

//对控件配置事件的处理
function configurationElm(e, t) {
    $(".demo").delegate("[controlType]", "dblclick", function (e) {
        var curCtrl = $(this);
        var curCT = curCtrl.attr("controlType");
        //手动清空弹出框的页面缓存，否则可能出现tab页签无法切换的bug,要求弹出框的最外层必须以myTab为ID  zhangtingting
        $("#myTab").remove();
        $.get("dj/ControlPropDialog/" + curCT + ".htm", function (ret) {
            $(ret).tabs().dialog({
                modal: true,             // 创建模式对话框
                title: curCT + " - 属性设计器",
                width: 700,
                height: 500,
                open: function () {
                    ShowProp(curCtrl, this);
                },
                buttons: {
                    "保存": function () {
                        SaveProp(curCtrl, this);
                        //ShowProp(curCtrl, this);//更改显示后即时刷新功能（radio、checkbox）
                        $(this).dialog("close");
                    },
                    "删除": function () {
                        curCtrl.remove();
                        $(this).dialog("close");
                    },
                    "关闭": function () {
                        $(this).dialog("close");
                    }
                }
            });
        });
        e.stopPropagation();
    });

    $(".demo").delegate("[controlType]", "click", function (e) {
        var curCtrl = $(this);
        curCtrl.toggleClass("control-selected");
        var i = $(".demo").find(".control-selected").length;
        if (i <= 1)
        {
            $(".demo").toggleClass("first");
        }
        //if (!e.ctrlKey)
        //{
        //    $(".demo").toggleClass("first");
            
        //}
        ////var i = $(".demo").find(".control-selected").length;
        ////curCtrl.toggleClass(i);
        //curCtrl.toggleClass("selected-no-" + $(".demo").find(".control-selected").length);  //测试
        //if (curCtrl.indexOf("selected-no-") = -1) {
        //    curCtrl.addClass("selected-no-" + $(".demo").find(".control-selected").length);
        //}
        ////else
        ////{
        ////    curCtrl.removeClass("selected-no-" + $(".demo").find(".control-selected").length);
        ////    curCtrl.addClass("selected-no-" + $(".demo").find(".control-selected").length);
        ////}
      
        ////按住Ctrl键为多选，按住为单选
        if (!e.ctrlKey) {
            $(".demo").find(".control-selected:not('#" + curCtrl.prop("id") + "')").removeClass("control-selected");
            
            $(".demo").find(".first:not('#" + curCtrl.prop("id") + "')").removeClass("first");
            curCtrl.toggleClass("first");
        }
        if ($(".demo").find(".control-selected").length > 0) {
            $("#poup").removeAttr("disabled");
            $("#podown").removeAttr("disabled");
        }
        else {
            $("#poup").attr("disabled", "disabled");
            $("#podown").attr("disabled", "disabled");
        }
        e.stopPropagation();
        $(".bootstrapMenu").hide();
    });

    $(".demo").delegate(".delete-table", "click", function (e) {
        var curCtrl = $(this).parent().parent();
        curCtrl.remove();
    });

}

$(window).resize(function () {
    $("body").css("min-height", $(window).height() - 90);
    $(".demo").css("min-height", $(window).height() - 160);
});

function restoreData() {
    if (supportstorage()) {
        layouthistory = JSON.parse(localStorage.getItem("layoutdata"));
        if (!layouthistory) return false;
        window.demoHtml = layouthistory.list[layouthistory.count - 1];
        if (window.demoHtml) $(".demo").html(window.demoHtml);
    }
}


//使当前表单为运行状态
function MakeHtmlRunable() {
    //取消控件单击绑定事件
    CancelControlEvent();
    //取消resizeable和dragable样式
    $("#mainContent").find("[controltype]").draggable('disable');
    $("#mainContent").find("[controltype]").resizable('disable');
    //隐藏表格这种日期控件后面的图标
    $("[controlType='table']").find("[controlType='date']").find("button").css("display", "none"); //.addClass("tabel_dateBtn");
  
    //设置label为不可编辑
    $("[controlType='label']").find("label").attr("contenteditable", false);
    $("[controlType='table']").find(".table-box").find("button").addClass("table-box-show");

}


/*
* 函数说明：单个按钮的对话框
* 参数说明：
*  parTitle： 对话框的标题，可以传null，默认为"提示信息"
*  parContent： 对话框的内容，为任意的HTML文本
*  parText： 确定按钮的文本，可以传null，默认为"确定"
*  parFunc： 点击确定按钮的时候回调函数，按钮默认关闭对话框
* 返回值：
*  执行modal之后的dom，可以在dom之后绑定自定义的js方法或事件
*/
function DialogOK(parTitle, parContent, parText, parFunc) {
    parTitle = (parTitle == null || parTitle == "") ? "提示信息" : parTitle;
    parText = (parText == null || parText == "") ? "确定" : parText;
    var modalStr = '<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">'
    + '<div class="modal-dialog">'
    + '<div class="modal-content">'
    + '<div class="modal-header">'
    + '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
    + '<h4 class="modal-title" id="myModalLabel">'
    + parTitle
    + '</h4></div>'
    + '<div class="modal-body">'
    + parContent
    + '</div><div class="modal-footer">'
    + '<button type="button" class="btn btn-success" data-dismiss="modal"><i class="fa fa-check"></i> '
    + parText
    + '</button>'
    + '</div></div></div></div></div>';
    var curModal = $(modalStr).modal();
    curModal.find(".btn-success").bind("click", parFunc);

    return curModal;
}

/*
* 函数说明：单个按钮的对话框的简单版
* 参数说明：
*  parContent： 对话框的内容，为任意的HTML文本
* 返回值：
*  执行modal之后的dom，可以在dom之后绑定自定义的js方法或事件
*/
function DialogOKSimple(parContent) {
    return DialogOK(null, parContent);
}

//使当前表单为编辑状态
function MakeHtmlEditable() {
    //重新绑定控件单击事件，先取消再绑定，否则会重复绑定，详见delegate的用法 sbin
    $(".demo").undelegate("[controlType]", "dblclick");
    $(".demo").undelegate("[controlType]", "click");
    $(".demo").undelegate(".delete-table", "click");
    for (var i = 0; i < $(".demo").find("[controlType]").length; i++) {
        $($(".demo").find("[controlType]")[i]).unbind("mousemove").unbind("mouseout");
        SetControlMouseMove($($(".demo").find("[controlType]")[i]));
    }
    //隐藏表格这种日期控件后面的图标
    $("[controlType='table']").find("[controlType='date']").find("button").css("display", "none"); //.addClass("tabel_dateBtn");
    configurationElm();

    //重新启动resizeable和dragable状态
    $("#mainContent").find("[controltype]").draggable("enable");
    $("#mainContent").find("[controltype]").resizable("enable");
}
var opArr = new Array();
var ipos = "";
//临时保存HTML 并限制数量
function SaveTemporHtml() {
    if (ipos != "") {
        $("#undo").removeAttr("disabled");
    }
    if (opArr.length < 10) {
        if (ipos == opArr.length) {
            opArr[opArr.length] = $("#mainContent").html();
            ipos = opArr.length;
        }
        else {
            opArr.splice(ipos, opArr.length - ipos);
            opArr[opArr.length] = $("#mainContent").html();
            ipos = opArr.length;
            $("#redo").attr("disabled", "disabled");
        }
    }
    else {
        //var a = opArr.shift();
        if (ipos == opArr.length) {
            var a = opArr.shift();
            opArr[opArr.length] = $("#mainContent").html();
            ipos = opArr.length;
        }
        else {
            opArr.splice(ipos, opArr.length - ipos);
            opArr[opArr.length] = $("#mainContent").html();
            ipos = opArr.length;
            $("#redo").attr("disabled", "disabled");
        }
    }

}
$(document).ready(function () {
    
    //设置高度
    $("body").css("min-height", $(window).height() - 90);
    $(".demo").css("min-height", $(window).height() - 160);

    //$("#mainContent").ruler();

    $("#mainContent").resizable();
    //拖拽事件
    $(".sidebar-nav .lyrow, .sidebar-nav .box").draggable({
        helper: "clone",
        handle: ".drag",
        start: function (e, t) {
            //SaveTemporHtml();
            InitCurC();
        },
        drag: function (e, t) {
            //t.helper.width(250);
        },
        stop: function (e, t) {
            var curCT = t.helper.find("[controlType]").attr("controlType");
            var ret = Add(curCT, t.helper);
            SaveTemporHtml();
        }
    });

    //初始化容器属性
    configurationElm();
    SaveTemporHtml();

    //编辑 按钮
    $("#edit").click(function () {
        $("body").removeClass("devpreview sourcepreview");
        $("body").addClass("edit");
        $(this).addClass("active");
        MakeHtmlEditable();
        $("#edit").attr("disabled", "disabled");
        $("#sourcepreview").removeAttr("disabled");
        return false
    });


    //预览 按钮
    $("#sourcepreview").click(function () {
        $("body").removeClass("edit");
        $("body").addClass("devpreview sourcepreview");
        //removeMenuClasses();
        $(this).addClass("active");
        MakeHtmlRunable();
        $("#sourcepreview").attr("disabled", "disabled");
        $("#edit").removeAttr("disabled");
        return false
    });

    $("#clear").click(function (e) {
        e.preventDefault();
        clearDemo()
    });
    $("#devpreview").click(function () {
        $("body").removeClass("edit sourcepreview");
        $("body").addClass("devpreview");
        //removeMenuClasses();
        $(this).addClass("active");
        //取消控件单击绑定事件
        CancelControlMouseMove();
        return false
    });

    $("#fluidPage").click(function (e) {
        e.preventDefault();
        changeStructure("container", "container-fluid");
        $("#fixedPage").removeClass("active");
        $(this).addClass("active");
        downloadLayoutSrc()
    });
    $("#fixedPage").click(function (e) {
        e.preventDefault();
        changeStructure("container-fluid", "container");
        $("#fluidPage").removeClass("active");
        $(this).addClass("active");
        downloadLayoutSrc()
    });
    $(".nav-header").click(function () {
        $(".sidebar-nav .boxes, .sidebar-nav .rows").hide();
        $(this).next().slideDown()
    });

    function SaveHtml(isOpen) {
        //首先把代码变成运行时代码
        $.get("dj/CRFModel/CRFBlank.htm", function (data) {
            var innerStr = data + $.trim($("#mainContent").html()) + "</body></html>";
            //savedHtml = $("#mainContent").html();
            //然后调用方法保存
            $.ajax({
                type: "POST",
                url: "FormDesign/SaveCRFHtm",
                data: { parFormId: GetParameter("parFID"), parFormHtm: escape(innerStr) },
                success: function (ret) {
                    if (ret.Result) {
                        if (isOpen) {
                            window.open("FormModule/" + GetParameter("parFID") + ".htm", "_blank");
                        } else {
                            DialogOKSimple("表单文件保存成功。");
                        }
                    } else {
                        DialogOKSimple("表单文件保存失败。");
                    }
                },
                error: function (r, s, e) {
                    DialogOKSimple("出错：" + e);
                }

            });
        });
    }



    $("#btnSave").click(function () {
        if (GetParameter("parFID") == null) {
            NewForm();
            return;
        }
        /**
        if ($("#edit").attr("disabled") == "disabled") {
            $("#sourcepreview").click();
            $("#sourcepreview").attr("disabled", "disabled");
        }
        */
        SaveHtml();
    });

    $("#btnOpenInExplorer").click(function () {
        if (GetParameter("parFID") == null) {
            NewForm();
            return;
        }
        //设置label为不可编辑
        $("[controlType='label']").find("label").attr("contenteditable", false);
        SaveHtml(true);
    });

    //撤销 按钮
    $("#undo").click(function () {
        if (ipos > 1) {
            ipos = ipos - 2;
            //$("#mainContent").html(opArr[ipos]);
            ReloadHtmlData(opArr[ipos]);
            $("#redo").removeAttr("disabled");
            ipos++;
            if (ipos <= 1) {
                $("#undo").attr("disabled", "disabled");
            }
        }
        else {
            //$("#mainContent").html(savedHtml);
            $("#undo").attr("disabled", "disabled");
        }
    });

    //重做 按钮
    $("#redo").click(function () {
        if (ipos == opArr.length) {
            $("#redo").attr("disabled", "disabled");
        }
        else {
            ReloadHtmlData(opArr[ipos]);
            //$("#mainContent").html(opArr[ipos]);
            $("#undo").removeAttr("disabled");
            ipos++;
            if (ipos >= opArr.length) {
                $("#redo").attr("disabled", "disabled");
            }
        }
    });

    //前置
    //var maxIndex = 100;
    //$("#poup").click(function () {
    //    for (var i = 0; i < $(".control-selected").length; i++) {
    //        if ($($(".control-selected")[i]).css("z-index") <= maxIndex) {
    //            $($(".control-selected")[i]).css("z-index", maxIndex + 1);
    //            maxIndex++;
    //        }
    //    }
    //});
    //var minIndex = 100;
    //$("#podown").click(function () {
    //    for (var i = 0; i < $(".control-selected").length; i++) {
    //        if ($($(".control-selected")[i]).css("z-index") >= minIndex) {
    //            $($(".control-selected")[i]).css("z-index", minIndex - 1);
    //            minIndex--;
    //        }
    //    }
    //});

    var LoopFindParent = function (curC) {
        var ret = null;
        if (curC.parent() != null) {
            if (curC.parent().attr("controlType") != "") {
                ret = curC.parent();
            } else {
                ret = LoopFindParent(curC.parent());
            }
        }
        return ret;
    }

    var podownFun = function () {
        var curC = $($(".control-selected")[0]);
        var curParent = LoopFindParent(curC);
        curParent.find("[controlType]").each(function (i, d) {
            if ($(d).prop("id") == curC.prop("id")) {
                $(d).css("z-index", "0")
            } else {
                if ($(d).css("z-index") != null && $(d).css("z-index") != "" && $(d).css("z-index") != "auto") {
                    $(d).css("z-index", parseInt($(d).css("z-index")) + 1)
                } else {
                    $(d).css("z-index", "1");
                }
            }
        });
        SaveTemporHtml();
    }

    var poupFun = function () {
        var curC = $($(".control-selected")[0]);
        var curParent = LoopFindParent(curC);
        var maxIndex = 0;
        curParent.find("[controlType]").each(function (i, d) {
            if ($(d).css("z-index") != null && $(d).css("z-index") != "" && $(d).css("z-index") != "auto") {
                var tmpIndex = parseInt($(d).css("z-index"));
                if (tmpIndex > maxIndex && $(d).prop("id") != curC.prop("id")) {
                    maxIndex = tmpIndex;
                }
            }
        });
        curC.css("z-index", maxIndex + 1);
        SaveTemporHtml();
    }

    $("#podown").click(function () {
        podownFun();
    });

    $("#poup").click(function () {
        poupFun();
    });

    //下载或预览源码
    $("#btnDownLoadSource").click(function () {
        $("#DownLoadSourceDialog").remove();
        $.get("dj/TooBarDialog/DownLoadModal.htm", function (data) {
            var innerStr = data;
            $(innerStr).dialog({
                modal: true,             // 创建模式对话框
                width: 800,
                height: 500,
                open: function () {
                    $(this).find("textarea").val($.trim($("#mainContent").html()));
                },
                buttons: {
                    "保存": function () { $(this).dialog("close"); },
                    "关闭": function () { $(this).dialog("close"); }
                }
            });
        });
    });

    //自定义函数
    $("#btnFormFunc").click(function () {
        //window.open(basePath + "/WebDesigner/dj/TooBarDialog/FormFuncModal.htm", "_blank");
        var ret = "<div style='z-index:9999999;top = 70px;'><iframe id='mainFrame' name='mainFrame' style='height:100%; visibility: inherit; width: 100%;z-index: -1;' scrolling='auto' frameborder='0' src='dj/TooBarDialog/FormFuncModal.htm'></iframe></div>";
        $(ret).dialog({
            modal: true,             // 创建模式对话框
            title: "自定义函数",
            width: 1100,
            height: 650,
            buttons: {
                "保存": function () {
                    $(this).dialog("close");
                },
                "关闭": function () { $(this).dialog("close"); }
            }
        });
    });

    function NewForm() {
        var ret = "<div style='z-index:9999999;top = 70px;'><iframe id='mainFrame' name='mainFrame' style='height:100%; visibility: inherit; width: 100%;z-index: -1;' scrolling='auto' frameborder='0' src='dj/TooBarDialog/FormInfoModal.htm'></iframe></div>";
        $(ret).dialog({
            modal: true,             // 创建模式对话框
            title: "表单命名",
            width: 500,
            height: 200,
            buttons: {
                "保存": function () {
                    var curFormName = frames["mainFrame"].document.getElementById("txtFormName").value;
                    if ($.trim(curFormName) != "") {
                        $.ajax({
                            type: "POST",
                            url: "FormDesign/AddNewForm",
                            data: { parFormName: escape(curFormName) },
                            success: function (ret) {
                                if (ret.Result) {
                                    window.open("Default.htm?parFID=" + ret.Data["ID"], "_self");
                                }
                            },
                            error: function (r, s, e) {
                                alert("出错：" + e);
                            },
                            async: false
                        });
                    } else {
                        alert("请输入表单名称！");
                    }


                    $(this).dialog("close");
                },
                "关闭": function () { $(this).dialog("close"); }
            }
        });
    }

    //新建表单
    $("#btnNew").click(function () {
        NewForm();
    });

    //数据类型和空间类型的映射表
    var DataTypeMapping = new Object();
    DataTypeMapping["text"] = "text";
    DataTypeMapping["int"] = "number";
    DataTypeMapping["number"] = "number";
    DataTypeMapping["date"] = "date";
    DataTypeMapping["time"] = "time";
    DataTypeMapping["datetime"] = "text";
    DataTypeMapping["dictionary"] = "dictionary";

    //通过容器的类型创建表单
    function CreateFormByCType(parCType) {
        var curFormID = GetParameter("parFID");
        if (curFormID == null || curFormID == "") { return; }
        var curForm = null;
        $.ajax({
            type: "POST",
            url: basePath + "/FormManage/GetById",
            data: { id: curFormID },
            success: function (msg) {
                if (msg.Result) {
                    //火狐下这个地方报错，暂未查明原因
                    curForm = msg.DataObject;
                }
            },
            error: function (r, s, e) {
                alert("出错：" + e);
            },
            async: false
        });

        var fcArr = null;
        $.ajax({
            type: "POST",
            url: basePath + "/ItemField/GetFormItemsByFID",
            data: { parFID: curFormID },
            success: function (msg) {
                if (msg.Result) {
                    fcArr = msg.DataObject;
                }
            },
            error: function (r, s, e) {
                alert("出错：" + e);
            },
            async: false
        });

        ////设置面板向下的长度
        $("#mainContent").css("height", (20 * fcArr.length + 50 + 100).toString() + "px");

        //先生成一个面板
        InitCurC();
        var divDom = $(".view").find("[controltype='" + parCType + "']").parent().parent().clone();
        var ret = Add(parCType, divDom);
        //设置面板的属性(这里是对象的引用，修改这里实际上就是修改了PanelArr中的对象)
        //ret.CName = curForm.CName;
        $("#" + ret.EName).attr("ConfigInfo", $.toJSON(ret));
        //如果是自动生成的，那么需要设置下面板的宽度
        $("#" + ret.EName).css("top", "0px");
        $("#" + ret.EName).css("left", "0px");
        $("#" + ret.EName).css("width", "840px");


        //var containerObj = $("#" + ret.EName); 
        //由于各种布局距离上边框的距离不一样，所以要根据布局类型设置
        var inputTopPadding = 0;
        if (parCType == "div") {
        } else if (parCType == "panel") {
            inputTopPadding = 50;
        } else if (parCType == "tabpanel") {
            inputTopPadding = 50;
        }

        var cuDynC = $("#" + ret.EName);
        var colCount = 0;   //记录表单和行数
        var tbColCount = 0;
        var labelTop = 0;
        var labelLeft = 0;
        var inputTop = 0;
        var inputLeft = 0;
        var unitLabelLeft = 0;
        var unitLabelTop = 0;
        var tableLeft = 30;
        var tableTop = 0;
        var num_i = 0;
        var table_H = 0;  //表格所占高度

        for (var i = 0; i < fcArr.length; i++) {
            //根据字段名称判断是否可以用表格形式显示控件
            var fieldnamestr = fcArr[i].Name.split("-");
            //fieldnamestr[0] = fieldnamestr[0] + ' (' + fcArr[i].Unit + ')';
            //单位
            var unitStr = (fcArr[i].Unit == null || fcArr[i].Unit == undefined || fcArr[i].Unit == "") ? "" : ' (' + fcArr[i].Unit + ')';
            if (fieldnamestr.length > 1) {
                if (rowFields.length > 0) {
                    //注意这里截取字符串' (' 有个空格
                    if (fieldnamestr[0] == rowFields[0].split(' (')[0]) {
                        colFields.push(fieldnamestr[1]);
                        mainContent.push(fcArr[i]);
                    }
                    else if (colFields.length > 1) {
                        if (rowFields.length > 0 && colFields.length > 1) {
                            AddTable(rowFields, colFields, mainContent);

                            rowFields = new Array();
                            colFields = new Array();
                            mainContent = new Array();
                        }
                        rowFields.push(fieldnamestr[0] + unitStr);
                        colFields.push(fieldnamestr[1]);
                        mainContent.push(fcArr[i]);

                    }
                    else if (colFields.length == 1) {
                        AddCurC(fcArr[i - 1]);

                        //添加一个控件
                        rowFields = new Array();
                        colFields = new Array();
                        mainContent = new Array();

                        rowFields.push(fieldnamestr[0] + unitStr);
                        colFields.push(fieldnamestr[1]);
                        mainContent.push(fcArr[i]);
                    }
                }
                else {
                    rowFields.push(fieldnamestr[0] + unitStr);
                    colFields.push(fieldnamestr[1]);
                    mainContent.push(fcArr[i]);
                }
            }
            else {
                if (rowFields.length > 0 && colFields.length > 1) {
                    AddTable(rowFields, colFields, mainContent);

                    rowFields = new Array();
                    colFields = new Array();
                    mainContent = new Array();
                }
                if (rowFields.length == 1 && colFields.length == 1) {

                    AddCurC(fcArr[i - 1]);
                    rowFields = new Array();
                    colFields = new Array();
                    mainContent = new Array();

                    AddCurC(fcArr[i]);
                } else {
                    AddCurC(fcArr[i]);
                }
            }
        }

        //判断最后一个控件
        if (rowFields.length > 0 && colFields.length > 0) {
            if (colFields.length > 1) {
                AddTable(rowFields, colFields, mainContent);
            } else {
                AddCurC(fcArr[i - 1]);
            }
        }

        if (Tables.length > 0) {
            var newParCurC = new parCurC();
            newParCurC.isTable = true;
            newParCurC.curCurC = Tables;

            curControlArr.push(newParCurC);
            Tables = new Array();
        }

        //添加控件
        for (var c = 0; c < curControlArr.length; c++) {
            if (curControlArr[c].isTable) {
                tableTop = 40 * colCount + 3 + inputTopPadding + table_H + 20;
                InitCurC();
                var divDom = $(".view").find("[controltype='table']").parent().parent().clone();
                cuDynC.attr("IsContainer", true);
                var ret = Add("table", divDom);

                SetTableDivAttr(ret, tableTop, tableLeft, curControlArr[c].curCurC[0], cuDynC);

                tbColCount += curControlArr[c].curCurC[0].rowFields.length + 1;
                table_H += 40 * curControlArr[c].curCurC[0].rowFields.length + 40 + 40; //40为表与前一个控件之间的间隔
                num_i = 0;
            }
            else {
                //计算label和input的top、left
                if (num_i % 2 == 0) {
                    labelLeft = 30;
                    inputLeft = 130;
                    labelTop = 40 * colCount + 12 + inputTopPadding + table_H;
                    inputTop = 40 * colCount + 3 + inputTopPadding + table_H;
                    unitLabelLeft = 340;
                    unitLabelTop = labelTop;

                    colCount++;
                } else {
                    labelLeft = 450;
                    inputLeft = 550;
                    unitLabelLeft = 760;
                }
                //添加元素
                AddElement(cuDynC, curControlArr[c].curCurC[0].fcArrObj, labelTop, labelLeft, inputTop, inputLeft, unitLabelTop, unitLabelLeft);
                num_i++;
            }
        }

        $("#mainContent").css("height", (40 * colCount + 50 + 100 + table_H).toString() + "px");
        //隐藏表格这种日期控件后面的图标
        $("[controlType='table']").find("[controlType='date']").find("button").css("display", "none"); //.addClass("tabel_dateBtn");

        //对div布局的特殊处理，去掉边框，上边距为15px(其他的布局如有特殊处理，请添加else)
        if (parCType == "div") {
            //下面默认留50的高度，每行留40的高度
            cuDynC.css("height", (40 * colCount + 50 + inputTopPadding + table_H).toString() + "px");
            //修改面板的边框(0px)
            cuDynC.css("border", "solid 0px");
            cuDynC.css("top", "15px");
        } else if (parCType == "panel") {
            inputTopPadding = 50;
            cuDynC.css("height", (40 * colCount + 50 + inputTopPadding + table_H).toString() + "px");
            cuDynC.find(".panel-body").css("height", (40 * colCount + inputTopPadding + table_H).toString() + "px");
            cuDynC.css("border", "none ");
        } else if (parCType == "tabpanel") {
            inputTopPadding = 50;
            cuDynC.css("height", (40 * colCount + 50 + inputTopPadding + table_H).toString() + "px");
            cuDynC.find(".tab-content").css("height", (40 * colCount + inputTopPadding + table_H).toString() + "px");
            //下面这个div的高度默认为100%，让其自适应，因为在tabpanel的resizeable()里，会自动计算计算高度
            //$("#" + ret.EName).find(".tab-content").find("div").css("height", (20 * fcArr.length).toString() + "px");
            cuDynC.find("[data-toggle]").text(curForm.CName);
        }

        InitCurC();
        SaveTemporHtml();
    }


    function AddCurC(fcArrObj) {

        if (Tables.length > 0) {
            var newParCurC = new parCurC();
            newParCurC.isTable = true;
            newParCurC.curCurC = Tables;

            curControlArr.push(newParCurC);
            Tables = new Array();
        }

        var newInputObj = new InputObj();
        newInputObj.fcArrObj = fcArrObj;
        Inputs.push(newInputObj);

        var newParCurC = new parCurC();
        newParCurC.isTable = false;
        newParCurC.curCurC = Inputs;
        curControlArr.push(newParCurC);
        Inputs = new Array();
    }

    //添加一个表格控件
    function AddTable(curRowFields, curColFields, mainContent) {
        var flg = 0;
        for (var t = 0; t < Tables.length; t++) {
            //判断是否可以合并为一个表格
            if (Tables[t].colFields.toString() == curColFields.toString()) {
                Tables[t].rowFields.push(curRowFields[0]); //表格新增一行
                for (var m = 0; m < mainContent.length; m++) {
                    Tables[t].mainContent.push(mainContent[m]);
                }
                flg = 1;
            }
        }
        if (flg == 0) {
            if (Tables.length > 0) {
                var newParCurC = new parCurC();
                newParCurC.isTable = true;
                newParCurC.curCurC = Tables;

                curControlArr.push(newParCurC);
                Tables = new Array();
            }

            //添加新的表格
            var newTableField = new TableField();
            newTableField.colFields = curColFields;
            newTableField.rowFields = curRowFields;
            newTableField.mainContent = mainContent;
            Tables.push(newTableField);
        }
    }

    //添加一个控件元素
    function AddElement(cuDynC, fcArrObj, labelTop, labelLeft, inputTop, inputLeft, unitLabelTop, unitLabelLeft) {
        //添加label控件
        InitCurC();
        var curCT = "label";
        var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
        cuDynC.attr("IsContainer", true);
        var ret = Add(curCT, divDom);
        ret.CName = escape(fcArrObj.Name);  //统一编码
        //要把自动生成的配置信息同步保存在ConfigInfo属性中 sbin 2016-4-1 请不要忘
        $("#" + ret.EName).attr("ConfigInfo", $.toJSON(ret));
        SetLableDivAttr(ret.EName, labelTop, labelLeft, fcArrObj.Name);

        //添加录入控件
        InitCurC();
        var curCT = DataTypeMapping[fcArrObj.DataType];
        var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
        cuDynC.attr("IsContainer", true);
        var ret = Add(curCT, divDom);
        ret.Code = escape(fcArrObj.Code);
        ret.DataFieldName = escape(fcArrObj.DataFieldName);
        ret.CName = escape(fcArrObj.Name);
        ret.Expression = escape(fcArrObj.Expression);
        ret.SingleSelect = escape(fcArrObj.SingleSelect);
        ret.DicTableID = escape(fcArrObj.DicTableID);
        ret.DicTableName = escape(fcArrObj.DicTableName);

        ////添加外部数据配置
        //var outConfig = new Object();
        //outConfig["RelatedAdm"] = fcArrObj.RelatedAdm;
        //outConfig["CateOutCode"] = fcArrObj.CateOutCode;
        //outConfig["ItemOutCode"] = fcArrObj.ItemOutCode;
        //outConfig["PropOutCode"] = fcArrObj.PropOutCode;

        //要把自动生成的配置信息同步保存在ConfigInfo属性中 sbin 2016-4-1 请不要忘
        $("#" + ret.EName).attr("ConfigInfo", $.toJSON(ret));
        SetInputDivAttr(ret.EName, inputTop, inputLeft, fcArrObj.Name, $.toJSON(ret), curCT);

        if (curCT != "date" && fcArrObj.Unit != "" && fcArrObj.Unit != null) {
            //添加单位 label控件
            InitCurC();
            var curCT = "label";
            var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
            cuDynC.attr("IsContainer", true);
            var ret = Add(curCT, divDom);
            ret.CName = escape(fcArrObj.Name);  //统一编码
            //要把自动生成的配置信息同步保存在ConfigInfo属性中 sbin 2016-4-1 请不要忘
            $("#" + ret.EName).attr("ConfigInfo", $.toJSON(ret));
            SetUnitLabelDivAttr(ret.EName, unitLabelTop, unitLabelLeft, fcArrObj.Unit);
        }
    }


    //设置table控件
    function SetTableDivAttr(retObj, pTop, pLeft, curTableObj, cuDynC) {
        var curObj = $("#" + retObj.EName);
        var rowFields = curTableObj.rowFields;
        var colFields = curTableObj.colFields;
        var fcArrObj = curTableObj.mainContent;
        var tableObj = $("#" + retObj.EName).find("table");
        //删除默认的表格
        if (tableObj[0].childNodes.length > 1) {
            tableObj[0].removeChild(tableObj[0].childNodes[1]);
        }

        var m = 0;
        for (var r = 0; r <= rowFields.length; r++) {
            tableObj.append('<tr id = "tr' + r + '">');
            for (var c = 0; c <= colFields.length; c++) {
                if (r == 0) {
                    if (c == 0) {
                        tableObj.find('#tr' + r).append(' <th style="width:100px; "> </th>');
                    } else {
                        //tableObj.find('#tr' + r).append(' <th>' + colFields[c - 1] + '</th>');
                        //添加表头
                        tableObj.find('#tr' + r).append('<th id="th' + c + '"></th>');
                        var parObj = tableObj.find('#tr' + r).find("#th" + c);
                        AddLabelToTable(colFields[c - 1], parObj, cuDynC);
                    }
                } else {
                    if (c == 0) {
                        //tableObj.find('#tr' + r).append(' <td style="width:100px;"> ' + rowFields[r - 1] + '</td>');
                        tableObj.find('#tr' + r).append(' <td style="min-width:100px;" id = "tdh' + c + '"></td>');
                        var parObj = tableObj.find('#tr' + r).find("#tdh" + c);
                        AddLabelToTable(rowFields[r - 1], parObj, cuDynC);
                    } else {
                        tableObj.find('#tr' + r).append('<td id="td' + c + '"></td>');
                        AddInputToTable(fcArrObj[m], tableObj.find('#tr' + r).find("#td" + c), cuDynC);

                        m++;
                    }
                }
            }
            tableObj.append('</tr>');

            //设置resizable和draggable属性
            SetResizableAndDraggable();

        }
        curObj.width(720);
        //curObj.find(".table-box").width(720);
        curObj.css("top", pTop);
        curObj.css("left", pLeft);
        var curDivH = 36 * rowFields.length + 43;
        curObj.height(curDivH);
        curObj.css("border", "#faf7f7 1px solid");

        retObj.row = curObj.find("table")[0].rows.length;
        retObj.col = curObj.find("table")[0].rows.item(0).cells.length;
        curObj.attr("ConfigInfo", $.toJSON(retObj));  //修改configinfo属性

        var tdObj = tableObj.find("td");
        tdObj.css("font-weight", "normal");
        tdObj.css("font-size", "13px");
        tdObj.css("color", "#666");
    }

    //设置label的DDIV的属性
    //宽度、坐标、text
    function SetLableDivAttr(lableID, pTop, pLeft, text) {
        var lableObj = $("#" + lableID);
        if (text.length > 7 && text.length <= 14) {
            pTop -= 6;
        } else if (text.length > 14) {
            pTop -= 12;
        }
        lableObj.width(90);
        lableObj.css("top", pTop);
        lableObj.css("left", pLeft);
        lableObj.find("label").html(text);

        //通用属性
        var label = lableObj.find("label");
        label.css("font-weight", "normal");
        label.css("font-size", "13px");
        label.css("color", "#666");
    }

    //往table空间中添加label
    function AddLabelToTable(labelCName, parObj, parDync) {
        InitCurC();
        var curCT = "label";
        var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
        parDync.attr("IsContainer", true);
        var ret = Add(curCT, divDom);
        ret.CName = labelCName;

        var InputObj = $("#" + ret.EName);
        InputObj.find("label").html(labelCName);
        InputObj.removeAttr("style");
        InputObj.removeClass("form-group");
        InputObj.attr("ConfigInfo", $.toJSON(ret));
        parObj.append(InputObj.clone());

        //通用属性
        var label = InputObj.find("label");
        label.css("font-weight", "normal");
        label.css("font-size", "13px");
        label.css("color", "#666");

        InputObj.remove();
    }

    //往table中添加一个输入控件
    function AddInputToTable(fcArrObj, parObj, parDync) {
        var curCT = DataTypeMapping[fcArrObj.DataType];
        var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
        parDync.attr("IsContainer", true);
        var ret = Add(curCT, divDom);
        ret.Code = fcArrObj.Code;
        ret.DataFieldName = fcArrObj.DataFieldName;
        ret.CName = fcArrObj.Name;
        ret.Expression = fcArrObj.Expression;
        ret.SingleSelect = fcArrObj.SingleSelect;
        ret.DicTableID = fcArrObj.DicTableID;
        ret.DicTableName = fcArrObj.DicTableName;

        var InputObj = $("#" + ret.EName);
        InputObj.removeAttr("style");
        InputObj.find("input,textarea").removeAttr("style");
        InputObj.find("input,textarea").removeAttr("placeholder");
        //InputObj.removeAttr("class");
        InputObj.removeClass("form-group");
        InputObj.attr("ConfigInfo", $.toJSON(ret));
        //对日期框特殊处理，隐藏日期框后面的按钮
        if (curCT == "date") {
            InputObj.find("button").css("display", "none");
        }
        parObj.append(InputObj.clone());

        InputObj.remove();
    }

    function SetInputDivAttr(controlID, pTop, pLeft, text, jsonStr, parCT) {
        var curWidth = 200;
        var controlObj = $("#" + controlID);
        controlObj.attr("ConfigInfo", jsonStr);
        //controlObj.attr("OutConfig", outStr);
        controlObj.width(curWidth);
        controlObj.css("top", pTop);
        controlObj.css("left", pLeft);
        if (parCT == "date") {
            controlObj.width(curWidth + 50);
            controlObj.find("input").css("width", curWidth);
        }
        if (parCT == "dictionary") {
            controlObj.find("select").find("option").remove();
            controlObj.find("select").multipleSelect("refresh");
        }
        controlObj.find("input,textarea").removeAttr("placeholder");
    }

    //设置控件单位的DDIV的属性
    function SetUnitLabelDivAttr(unitLabelID, pTop, pLeft, text) {
        var lableObj = $("#" + unitLabelID);
        //设置单位的宽度
        lableObj.width(50);
        lableObj.css("top", pTop);
        lableObj.css("left", pLeft);
        lableObj.find("label").html(text);

        //通用属性
        var label = lableObj.find("label");
        label.css("font-weight", "normal");
        label.css("font-size", "13px");
        label.css("color", "#666");
    }

    //设置控件为可拖拽
    function SetResizableAndDraggable() {
        $("#mainContent").resizable();
        $("#mainContent").find("[controltype='label'], [controltype='text'], [controltype='button'], [controltype='number'], [controltype='date'], [controltype='time'], [controltype='email'], [controltype='dictionary']").resizable({
            containment: "parent", handles: "e",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });

        //对Table 的处理
        $("#mainContent").find("[controltype='table']").resizable({
            containment: "parent", handles: "se",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });

        $("#mainContent").find("[controltype]").draggable({
            containment: "parent",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
    };

    function ReloadHtmlData(data) {
        //在获取的数据的外部添加了mainContent容器的的html代码，请不要修改,除非你找到更好的办法解决下面的问题
        //这样做是有原因的：如果不是动态拼的话，页面自带的mainContent在改变了html之后是不能resizeable的  shebin amend 2016-3-1
        var len = data.split("<body>").length;
        var innerHtml = "";
        if (len == 2) {
            innerHtml = '<div style="min-height: 700px; padding-top:10px;" id="mainContent" class="demo ui-draggable ui-sortable">' + data.split("<body>")[1].replace("</body></html>", "").replace(/\r\n/g, "") + '</div>';
        } else {
            innerHtml = '<div style="min-height: 700px; padding-top:10px;" id="mainContent" class="demo ui-draggable ui-sortable">' + data + '</div>';
        }
        $("#mainContent").parent().html(innerHtml);
        $("#mainContent .ui-resizable-handle").remove();
        $("#mainContent").resizable();

        //设置默认高度，以内部面板的高度加上100为准
        for (var i = 0; i < $("#mainContent")[0].childNodes.length; i++) {
            var tmpct = $($("#mainContent")[0].childNodes[i]).attr("controlType");
            if (tmpct == "div" || tmpct == "panel" || tmpct == "tabpanel" || tmpct == "table") {
                $("#mainContent").css("height", (parseInt($($("#mainContent")[0].childNodes[i]).css("height").replace("px", "")) + 100).toString() + "px");
            }
        }

        //重新设置所有控件为可拖拽，可改变大小,不同的控件有不同的特殊处理方式
        //对tabpanel的特殊处理
        $("#mainContent").find("[controltype='tabpanel']").resizable({
            containment: "parent",
            resize: function (e, ui) {
                ui.element.find(".tab-content").css("height", (ui.element.css("height").replace("px", "") - 45) + "px");
            },
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });

        //对div、panel的处理
        $("#mainContent").find("[controltype='panel'], [controltype='div'], [controltype='img'],[controltype='radio'],[controltype='checkbox'],[controltype='shape']").resizable({
            containment: "parent", handles: "se",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        //对其他控件的处理
        $("#mainContent").find("[controltype='label'],[controltype='line'], [controltype='text'], [controltype='button'], [controltype='number'], [controltype='date'], [controltype='time'], [controltype='email'], [controltype='dictionary'], [controltype='select']").resizable({
            containment: "parent", handles: "e",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        //对Table 的处理
        $("#mainContent").find("[controltype='table']").resizable({
            containment: "parent", handles: "se",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        //$("#mainContent").find("[controltype='table']").find("[controltype='label'], [controltype='text'], [controltype='button'], [controltype='number'], [controltype='date'], [controltype='time'], [controltype='email'], [controltype='dictionary']").resizable({ containment: "parent", handles: "e" });

        $("#mainContent").find("[controltype]").draggable({
            containment: "parent",
            stop: function (e, t) {
                SaveTemporHtml();
            }
        });
        //设置界面为可编辑状态，和自动生成的界面保持一致
        MakeHtmlEditable();

    }
    //解析参数，判断生成那种类型的界面
    function AutoInitForm() {
        //先判断以前是否保存过，如果以前保存了，就加载以前的，否则再创建
        var curFormID = GetParameter("parFID");
        $.ajax({
            type: "POST",
            url: basePath + "/FormDesign/IsCRFExists",
            data: { parFormId: curFormID },
            success: function (ret) {
                if (ret.Result) {
                    //如果以前保存了，先加载以前的表单，然后再判断是否有字段新增或删除
                    $.get("FormModule/CRF/" + curFormID + ".htm", function (data) {
                        ReloadHtmlData(data);
                        var curFType = GetParameter("parFType");
                        var parCType = "";
                        switch (curFType) {
                            case "1":
                                {
                                    parCType = "div";
                                    break;
                                }
                            case "2":
                                {
                                    parCType = "tabpanel";
                                    break;
                                }
                        }
                        ReferenControl(parCType);
                    });
                } else {
                    var curFType = GetParameter("parFType");
                    switch (curFType) {
                        case "1":
                            {
                                CreateFormByCType("div");
                                break;
                            }
                        case "2":
                            {
                                CreateFormByCType("tabpanel");
                                break;
                            }
                    }
                }
            },
            error: function (r, s, e) {
                alert("出错：" + e);
            },
            async: false
        });

        $("[controlType='table']").find(".table-box").find("button").addClass("table-box-show");
    }

    //刷新字段，新增的字段在已有表单下方补充
    function ReferenControl(parCType) {
        var curAddFcArr = window.opener.window.addFcArr;  //获取新增的字段
        var curDelFcArr = window.opener.window.delFcArr;
        var curUpdateFcArr = window.opener.window.updateFcArr;
        var cuDynC = $("#mainContent").find("[controltype='div']");

        //计算label和input的top、left
        var labelLeft = 0;
        var inputLeft = 0;
        var labelTop = 0;
        var inputTop = 0;
        var unitLabelLeft = 0;
        var unitLabelTop = 0;
        //获取最后一个元素的top和height
        var curTop = 0;
        var curHeight = 0;
        var colCount = 1;
        var curObj = $("#mainContent").find("[controltype]");
        //var curObj = $("#mainContent").find("[controltype='label'],[controltype='text'], [controltype='table'], [controltype='button'], [controltype='number'], [controltype='date'], [controltype='time'], [controltype='email'], [controltype='dictionary']")
        for (var i = 0; i < curObj.length; i++) {
            var top = parseInt(curObj[i].style.top.replace("px", ""));
            if (top > curTop) {
                if (curObj.attr("controltype") == "table" || curObj.attr("controltype") == "div") {
                    curHeight = parseInt(curObj[i].style.height.replace("px", "")) + 20;
                    colCount = 0;
                } else {
                    curHeight = 0;
                }
                curTop = top;
            }
        }
        curTop = curTop + curHeight - 12;
        //新增  新增前需要判断是否已经存在
        if (curAddFcArr.length > 0) {
            for (var i = 0; i < curAddFcArr.length; i++) {
                if (i % 2 == 0) {
                    labelLeft = 30;
                    inputLeft = 130;
                    labelTop = curTop + colCount * 40 + 12;
                    inputTop = curTop + colCount * 40 + 3;
                    unitLabelLeft = 340;
                    unitLabelTop = labelTop;
                } else {
                    labelLeft = 450;
                    inputLeft = 550;
                    unitLabelLeft = 760;
                    colCount++;
                }

                //添加元素
                AddElement(cuDynC, curAddFcArr[i], labelTop, labelLeft, inputTop, inputLeft, unitLabelTop, unitLabelLeft);
            }

            $("#mainContent").css("height", (curTop + 40 * colCount + 60 + 100).toString() + "px");
            //对div布局的特殊处理，去掉边框，上边距为15px(其他的布局如有特殊处理，请添加else)
            if (parCType == "div") {
                //下面默认留50的高度，每行留40的高度
                cuDynC.css("height", (curTop + 40 * colCount + 60).toString() + "px");
                //修改面板的边框(0px)
                cuDynC.css("border", "solid 0px");
                cuDynC.css("top", "15px");
            } else if (parCType == "panel") {
                inputTopPadding = 50;
                cuDynC.css("height", (curTop + 40 * colCount + 60).toString() + "px");
                cuDynC.find(".panel-body").css("height", (curTop + 40 * colCount).toString() + "px");
                cuDynC.css("border", "none ");
            } else if (parCType == "tabpanel") {
                inputTopPadding = 50;
                cuDynC.css("height", (curTop + 40 * colCount + 60).toString() + "px");
                cuDynC.find(".tab-content").css("height", (curTop + 40 * colCount).toString() + "px");
                //下面这个div的高度默认为100%，让其自适应，因为在tabpanel的resizeable()里，会自动计算计算高度
                //$("#" + ret.EName).find(".tab-content").find("div").css("height", (20 * fcArr.length).toString() + "px");
                cuDynC.find("[data-toggle]").text(curForm.CName);
            }

            InitCurC();
            SaveTemporHtml();
        }
        //删除
        if (curDelFcArr.length > 0) {
            for (var i = 0; i < curDelFcArr.length; i++) {
                var conStr = escape(curDelFcArr[i].Name).toString();
                var fieldControls = cuDynC.find("*[configinfo]");
                fieldControls.each(function (index, d) {
                    var thisEl = $(d);
                    var configStr = $(d).attr("configinfo");
                    if (configStr) {
                        var curConfigObj = eval("(" + configStr + ")");
                        if (curConfigObj.CName == conStr) {
                            thisEl.remove();
                        }
                    }
                });
            }
        }

        //修改
        if (curUpdateFcArr.length > 0) {
            for (var i = 0; i < curUpdateFcArr.length; i++) {
                var fcArrObj = curUpdateFcArr[i];
                var corCodeStr = curUpdateFcArr[i].Code;
                var corNameStr = escape(curUpdateFcArr[i].Name).toString();
                var fieldControls = cuDynC.find("*[configinfo]");
                fieldControls.each(function (index, d) {
                    var thisEl = $(d);
                    var configStr = $(d).attr("configinfo");
                    var conCType = $(d).attr("controltype");
                    if (configStr) {
                        var curConfigObj = eval("(" + configStr + ")");
                        if (curConfigObj.CName == corNameStr && conCType == "label") {
                            thisEl.remove();
                        }
                        if (curConfigObj.Code == corCodeStr) { //找到对应的input
                            curInputObj = thisEl; //认为只存在一个
                            //判断是否在表格中
                            if (thisEl.parent()[0].nodeName == "TD" || thisEl.parent()[0].nodeName == "TH") {
                                var parObj = thisEl.parent();
                                thisEl.remove();
                                parObj.each(function (index, t) {
                                    AddInputToTable(curUpdateFcArr[i], $(t), cuDynC);
                                })
                            } else {
                                //获取原始input的top 和left，表格控件中的input是没有top和left属性的
                                inputTop = parseInt(thisEl[0].style.top.replace("px", ""));
                                inputLeft = parseInt(thisEl[0].style.left.replace("px", ""));
                                labelTop = inputTop + 9;
                                labelLeft = inputLeft - 100;
                                unitLabelTop = labelTop;
                                unitLabelLeft = inputLeft + 210;
                                thisEl.remove();

                                //然后再添加新类型元素
                                AddElement(cuDynC, curUpdateFcArr[i], labelTop, labelLeft, inputTop, inputLeft, unitLabelTop, unitLabelLeft);
                            }
                            SetResizableAndDraggable();
                        }
                    }
                });
            }
        }
    }

    //AutoInitForm();

    function loadForm() {
        $.ajax({
            type: "POST",
            url: "FormDesign/GetAllForm",
            data: {},
            success: function (ret) {
                if (ret.Result) {
                    var innerStr = '';
                    for (var i = 0; i < ret.Data.length; i++) {
                        innerStr += '<li role="presentation" fid=' + ret.Data[i].ID + '><a role="menuitem" tabindex="-1" href="#">' + ret.Data[i].FormName + '</a></li>';
                    }

                    $("#fMenu").html(innerStr);
                    $("#fMenu").find("li").click(function () {
                        window.open("Default.htm?parFID=" + $(this).attr("fid"), "_self");
                    });
                }
            },
            error: function (r, s, e) {
                alert("加载表单菜单出错：" + e);
            },
            async: false
        });

        var curFormID = GetParameter("parFID");
        if (curFormID != null && curFormID != "") {
            $.ajax({
                type: "POST",
                url: "FormDesign/GetFormByID",
                data: { parID: curFormID },
                success: function (ret) {
                    if (ret.Result) {
                        $("#lbFormName").text("当前表单：" + ret.Data["FormName"]);
                        if (ret.Data["IsCreate"] == "1") {
                            $.get("FormModule/" + curFormID + ".htm", function (retStr) {
                                ReloadHtmlData(retStr);
                            });
                        }
                    }
                },
                error: function (r, s, e) {
                    //alert("加载表单菜单出错：" + e);
                },
                async: false
            });
        }
    }
    loadForm();

    //对于动态生成的html,使用delegate绑定事件，防止失效  shebin amend 2016-4-28
    $(document).delegate("#mainContent", "click", function (e) {
        $(".control-selected").removeClass("control-selected");
    });

    //这里还存在问题，保存后再次打开设计时失效
    //$("#mainContent").click(function () {
    //    debugger;
    //    $(".control-selected").removeClass("control-selected");
    //});

    var ClipboardArr = new Array();;
    function DoCopy() {
        //每次复制之前将上一次的记录清空
        ClipboardArr = [];
        $(".control-selected").each(function (i, d) {
            ClipboardArr.push(curID);
        });
    }

    //粘贴操作  shebin amend 2016-4-28
    function DoPaste() {
        if (ClipboardArr.length < 1) {
            return false;
        }
        //初始化容器
        InitCurC();
        //判断当前选择的是不是容器控件
        if ($(".demo").find(".control-selected").length == 1) {
            var curSelCT = $(".demo").find(".control-selected").attr("controlType");
            for (var i = 0; i < ClipboardArr.length; i++) {
                var cloneObj = $("#" + ClipboardArr[i]).clone(false);
                cloneObj.css("top", (parseInt(cloneObj.css("top").replace("px", "")) + 20) + "px");
                cloneObj.find(".ui-resizable-handle").remove();
                //在放到容器之前，要重新设置控件的ID以及在全局数组中记录， shebin amend 
                cloneObj
                switch (curSelCT) {
                    case "div":
                        {
                            $(".demo").find(".control-selected").append(cloneObj);
                            break;
                        }
                    case "panel":
                        {
                            $(".demo").find(".control-selected").find(".panel-body").append(cloneObj);
                            break;
                        }
                    case "tabpanel":
                        {
                            $(".demo").find(".control-selected").find(".tab-pane.active").append(cloneObj);
                            break;
                        }
                }

                //粘贴完成之后，需要重新设置控件的可拖拽和可改变大小  shebin amend
            }
        }

    }
    
    //右键菜单
    var menu = new BootstrapMenu("[controlType]", {
        fetchElementData: function ($CTElem) {
            var CT = $CTElem;
            return CT;
        },
        actionsGroups: [
            ["前置", "后置"],
            ["左对齐", "右对齐"],
            ["上对齐", "下对齐"],
            ["水平居中","垂直居中"],
            ["撤销", "重做"],
            ["删除"],
            ["取消"]
        ],
        actions: {
            前置: {
                name: "前置",
                iconClass: "fa-chevron-up",
                onClick: function (e) {
                    if ($(".control-selected").length > 0) {
                        poupFun();
                    }
                    else {
                        e.addClass("control-selected");
                        poupFun();
                    }
                    $("#poup").removeAttr("disabled");
                    $("#podown").removeAttr("disabled");
                }
            },
            后置: {
                iconClass: "fa-chevron-down",
                name: "后置",
                onClick: function (e) {
                    if ($(".control-selected").length > 0) {
                        podownFun();
                    }
                    else {
                        e.addClass("control-selected");
                        podownFun();
                    }
                    $("#poup").removeAttr("disabled");
                    $("#podown").removeAttr("disabled");
                }
            },
            删除: {
                iconClass: "fa-trash-o",
                name: "删除",
                onClick: function (e) {
                    $(".control-selected").remove();
                    e.remove();
                }
            },
            撤销: {
                name: "撤销",
                iconClass: "fa-rotate-left",
                onClick: function () {
                    $("#undo").click();
                },
                isEnabled: function () {
                    //return row.isEditable;
                    if ($("#undo").attr("disabled") == "disabled") {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
            },
            重做: {
                name: "重做",
                iconClass: "fa-rotate-right",
                onClick: function () {
                    $("#redo").click();
                },
                isEnabled: function () {
                    if ($("#redo").attr("disabled") == "disabled") {
                        return false;
                    }
                    else {
                        return true;
                    }
                }
            },
            取消: {
                name: "取消",
                iconClass: "fa-times",
                onClick: function () {
                }
            },
            左对齐: {
                name: "左对齐",
                iconClass: "fa-align-left",
                onClick: function () {
                    if ($(".demo").find(".control-selected").length > 1) {
                        //var cur=$(".demo").find(".control-selected");
                        //var res = $(cur[0]).css("left");
                        //$($(cur[0]).parent()).find(".control-selected").css("left", res);
                        var cur = $(".demo").find(".first");
                        var res = cur.css("left");
                        cur.parent().find(".control-selected").css("left", res);
                    }
                }
            },
            右对齐: {
                name: "右对齐",
                iconClass: "fa-align-right",
                onClick: function () {
                    if ($(".demo").find(".control-selected").length > 1) {                       
                        var cur = $(".demo").find(".first");
                        var curAll = $(".demo").find(".control-selected");
                        var left1 =cur.css("left").replace("px","");
                        var wid1=cur.css("width").replace("px","");
                        for (var i = 0; i < curAll.length; i++) {
                            var wid = $(curAll[i]).css("width").replace("px", "");
                            var res = parseInt(left1) + parseInt(wid1) - parseInt(wid);
                            $(cur.parent().find(".control-selected")[i]).css("left", res);
                        }
                    }
                }
            },
            上对齐: {
                name: "上对齐",
                iconClass: "fa-arrow-circle-up",
                onClick: function () {
                    if ($(".demo").find(".control-selected").length > 1) {
                        var cur = $(".demo").find(".first");
                        var res = cur.css("top");
                        cur.parent().find(".control-selected").css("top", res);
                    }
                }
            },
            下对齐: {
                name: "下对齐",
                iconClass: "fa-arrow-circle-down",
                onClick: function () {
                    //debugger;
                    if ($(".demo").find(".control-selected").length > 1) {
                        var cur = $(".demo").find(".first");
                        var curAll = $(".demo").find(".control-selected");
                        var top1 = cur.css("top").replace("px", "");
                        var hei1 = cur.css("height").replace("px", "");
                        for (var i = 0; i < curAll.length; i++) {
                            var hei = $(curAll[i]).css("height").replace("px", "");
                            var res = parseInt(top1) + parseInt(hei1) - parseInt(hei);
                            $(cur.parent().find(".control-selected")[i]).css("top", res);
                        }
                    }
                }
            },

            水平居中: {
                name: "水平居中",
                iconClass: "fa-align-justify",
                onClick: function () {
                    if ($(".demo").find(".control-selected").length > 1) {
                        //var cur = $(".demo").find(".control-selected");
                        //var left1 = parseInt($(cur[0]).css("left").replace("px", ""));
                        //var wid1 = parseInt($(cur[0]).css("width").replace("px", ""));
                        //for (var i = 1; i < cur.length; i++) {
                        //    if ($(cur[i]).next().length > 0)
                        //    {
                        //        var wid = parseInt($(cur[i]).css("width").replace("px", ""));
                        //        var res = left1 + (wid1 / 2) - (wid / 2);
                        //        $(cur[i]).css("left", res + "px");
                        //    }
                        //}
                        var cur = $(".demo").find(".first");
                        var curAll = $(".demo").find(".control-selected");
                        var left1 = cur.css("left").replace("px", "");
                        var wid1 = cur.css("width").replace("px", "");                       
                        for (var i = 0; i < curAll.length; i++) {
                            var wid = $(curAll[i]).css("width").replace("px", "");
                            var res = parseInt(left1) + parseInt(wid1) / 2 - parseInt(wid) / 2;
                            $(cur.parent().find(".control-selected")[i]).css("left", res);
                        }
                    }
                }
            },
            垂直居中: {
                name: "垂直居中",
                iconClass: "fa-align-center",
                onClick: function () {
                    if ($(".demo").find(".control-selected").length > 1) {
                        //var cur = $(".demo").find(".control-selected");
                        //var top1 = parseInt($(cur[0]).css("top").replace("px",""));
                        //var hei1 = parseInt($(cur[0]).css("height").replace("px",""));
                        //for (var i = 0; i < cur.length; i++) {
                        //    if ($(cur[i]).next().length > 0)
                        //    {
                        //        var hei = parseInt($(cur[i]).css("height").replace("px", ""))
                        //        var res = top1 + (hei1 / 2) - (hei / 2);
                        //        $(cur[i]).css("top", res + "px");
                        //    }
                        //}
                        var cur = $(".demo").find(".first");
                        var curAll = $(".demo").find(".control-selected");
                        var top1 = cur.css("top").replace("px", "");
                        var hei1 = cur.css("height").replace("px", "");
                        
                        for (var i = 0; i < curAll.length; i++) {
                            var hei = $(curAll[i]).css("height").replace();
                            var res = parseInt(top1) + parseInt(hei1) / 2 - parseInt(hei) / 2;
                            
                            $(cur.parent().find(".control-selected")[i]).css("top", res);
                        }
                    }
                }
            },
        }
    });
    
    //表格右键菜单
    var tabMenu = new BootstrapMenu("[id^='TBL'] tr td", {
        fetchElementData: function ($CTElem) {
            var CT = $CTElem;
            return CT;
        },
        actionsGroups: [  //给右键菜单的选项加一个分组，分割线
            ["新增控件"],
            ["插入一行", "插入一列"],
            ["删除一行", "删除一列"],
            ["拆分单元格", "合并单元格"],
            ["删除单元格"],
            ["取消"]
        ],
        actions: {
            新增控件:{
                iconClass:"",
                name: "插入控件",
                onClick: function (e) {
                    //当前单元格位置
                    var curRowsIndex = e.parent()[0].rowIndex;
                    var curCellsIndex = e[0].cellIndex;
                    var curTabObj = e.parent().parent()[0];
                    var curCtrl = e.parent().parent().parent().parent();
                    var curCT = curCtrl.attr("controlType");
                    //手动清空弹出框的页面缓存，否则可能出现tab页签无法切换的bug,要求弹出框的最外层必须以myTab为ID 
                    $("#myTab").remove();
                    $.get("dj/ControlPropDialog/addTableEL.htm", function (ret) {
                        $(ret).tabs().dialog({
                            modal: true,             // 创建模式对话框
                            title: curCT + " - 新增控件",
                            width: 700,
                            height: 500,
                            open: function () {
                                $(this).find("#txtIndex").val((curRowsIndex + 1) + "行" + (curCellsIndex + 1) + "列");
                                var controlList = $(this).find("#controlList");
                                var selStyleObj = $(this).find("#selType");
                                $(this).find("#controlSource").change(function () {
                                    controlList[0].options.length = 0; //删除所有，然后重新创建

                                    controlList.val("");

                                    if (this.value == "新建控件") {
                                        //alert("新建控件");
                                        //显示控件类型
                                        selStyleObj.parent()[0].style.display = "block";
                                        selStyleObj.change(function () {
                                            if (this.value == "录入控件") {
                                                //获取录入分类下控件列表
                                                var conlist = $("#inputControl").find("*[controlType]");
                                                
                                                $.each(conlist, function (i, item) {
                                                    controlList[0].options.add(new Option($(this).attr("controlType"), $(this).attr("controlType")));
                                                });
                                            } else if (this.value == "界面控件") {
                                                //获取界面分类下控件列表
                                                var conlist = $("#viewControl").find("*[controlType]");
                                                controlList[0].options.length = 0; //删除所有，然后重新创建
                                                $.each(conlist, function (i, item) {
                                                    controlList[0].options.add(new Option($(this).attr("controlType"), $(this).attr("controlType")));
                                                });
                                            }
                                        });
                                    } else {
                                        selStyleObj.parent()[0].style.display = "none";
                                        //获取界面已有的控件列表
                                        var conlist = $("#mainContent").find("*[controlType]");
                                        $.each(conlist, function (i, item) {
                                            if ($(this).attr("controlType") != "table" && $(this).attr("controlType") != "div") {
                                                var configinfo = eval("(" + $(this).attr("ConfigInfo") + ")");
                                                controlList[0].options.add(new Option(unescape(configinfo.CName) + "(" + configinfo.EName + ")",
                                                    configinfo.EName));
                                            }
                                        });
                                    }
                                });
                            },
                            buttons: {
                                "保存": function () {
                                    e[0].innerHTML = ""; //清空原先的
                                    var controlSourceStr = $(this).find("#controlSource").val();
                                    if (controlSourceStr == "新建控件") {
                                        //添加录入控件
                                        InitCurC();
                                        var cuDynC = $("#mainContent").find("[controltype='div']");
                                        var curObj = $(this).find("#controlList")[0];
                                        //var index = curObj.selectedIndex; // 选中索引
                                        //var curCT = curObj.options[index].text   //选中的文本
                                        var curCT = $(this).find("#controlList").val();  //获取值
                                        var divDom = $(".view").find("[controltype='" + curCT + "']").parent().parent().clone();
                                        //cuDynC.attr("IsContainer", true);
                                        var ret = Add(curCT, divDom);

                                        var InputObj = $("#" + ret.EName);
                                        InputObj.removeAttr("style");
                                        InputObj.removeClass("form-group");
                                        InputObj.find("input,textarea").removeAttr("style");
                                        InputObj.find("input,textarea").removeAttr("placeholder");
                                        //对日期控件特殊处理  
                                        if (curCT == "date") {
                                            InputObj.find("button").css("display", "none"); //隐藏日期框后面的按钮
                                        }
                                        InputObj.attr("ConfigInfo", $.toJSON(ret));
                                        e.append(InputObj.clone());

                                        if (curCT == "label") {
                                            //通用属性
                                            var label = InputObj.find("label");
                                            label.css("font-weight", "normal");
                                            label.css("font-size", "13px");
                                            label.css("color", "#666");
                                        }
                                        InputObj.remove();
                                        SetResizableAndDraggable();
                                    
                                    } else {
                                        var curEName = $(this).find("#controlList").val();  //获取值
                                        var curObj = $("#" + curEName);
                                        
                                        curObj.removeAttr("style");
                                        curObj.find("input,textarea").removeAttr("style");
                                        curObj.find("input,textarea").removeAttr("placeholder");
                                        curObj.removeClass("form-group");
                                        //对日期控件特殊处理
                                        if ($("#" + curEName).attr("controltype") == "date") {
                                            curObj.find("button").css("display", "none");
                                        }
                                        e.append(curObj);
                                    }
                                    $(this).dialog("close");
                                },
                                "删除": function () {
                                    curCtrl.remove();
                                    $(this).dialog("close");
                                },
                                "关闭": function () {
                                    $(this).dialog("close");
                                }
                            }
                        });
                    });
                }
            },
            插入一行: {
                iconClass: "fa-add-o",
                name: "插入一行",
                onClick: function (e) {
                    var curTabObj = e.parent().parent()[0];
                    var curDivCT = e.parent().parent().parent().parent();
                    addTableRow(curTabObj, e.parent()[0].rowIndex+1);

                    //修改ConfigInfo
                    var ctrlInfo = eval("(" + curDivCT.attr("ConfigInfo") + ")");
                    ctrlInfo.row = parseInt(ctrlInfo.row) + 1;
                    //保存数据到控件上
                    curDivCT.attr("ConfigInfo", $.toJSON(ctrlInfo));
                    //设置div 的height 和width
                    var curDivH = curTabObj.offsetHeight + 3;  //修改表单外面div的height
                    curDivCT.height(curDivH);
                }
            },
            插入一列: {
                iconClass: "fa-add-o",
                name: "插入一列",
                onClick: function (e) {
                    var curTabObj = e.parent().parent()[0];
                    var curDivCT = e.parent().parent().parent().parent();
                    addTableCell(curTabObj, e[0].cellIndex+1);
                    //修改ConfigInfo
                    var ctrlInfo = eval("(" + curDivCT.attr("ConfigInfo") + ")");
                    ctrlInfo.col = parseInt(ctrlInfo.col) + 1;
                    curDivCT.attr("ConfigInfo", $.toJSON(ctrlInfo));
                    //设置div 的height 和width
                    var curDivH = curTabObj.offsetHeight + 3;  //修改表单外面div的height
                    curDivCT.height(curDivH);
                }
            },
            删除一行: {
                iconClass: "fa-del-o",
                name: "删除一行",
                onClick: function (e) {
                    var curTabObj = e.parent().parent()[0];
                    var curDivCT = e.parent().parent().parent().parent();
                    removeTableRow(curTabObj, e.parent()[0].rowIndex);
                    //修改ConfigInfo
                    var ctrlInfo = eval("(" + curDivCT.attr("ConfigInfo") + ")");
                    ctrlInfo.row = parseInt(ctrlInfo.row) - 1;
                    //保存数据到控件上
                    curDivCT.attr("ConfigInfo", $.toJSON(ctrlInfo));
                    //设置div 的height 和width
                    var curDivH = curTabObj.offsetHeight + 3;  //修改表单外面div的height
                    curDivCT.height(curDivH);
                }
            },
            删除一列: {
                iconClass: "fa-del-o",
                name: "删除一列",
                onClick: function (e) {
                    var curTabObj = e.parent().parent()[0];
                    var curDivCT = e.parent().parent().parent().parent();
                    removeTableCell(curTabObj, e[0].cellIndex);
                    //修改ConfigInfo
                    var ctrlInfo = eval("(" + curDivCT.attr("ConfigInfo") + ")");
                    ctrlInfo.col = parseInt(ctrlInfo.col) - 1;
                    curDivCT.attr("ConfigInfo", $.toJSON(ctrlInfo));
                    //设置div 的height 和width
                    var curDivH = curTabObj.offsetHeight + 3;  //修改表单外面div的height
                    curDivCT.height(curDivH);
                }
            },
            拆分单元格: {
                iconClass: "fa-add-o",
                name: "拆分单元格",
                onClick: function (e) {
                }
            },
            合并单元格: {
                iconClass: "fa-add-o",
                name: "合并单元格",
                onClick: function (e) {
                }
            },
            删除单元格: {
                iconClass: "fa-trash-o",
                name: "删除单元格",
                onClick: function (e) {
                    e.remove();
                }
            },
            取消: {
                name: "取消",
                iconClass: "fa-times",
                onClick: function () {
                }
            }
        }
    });

    //表格中控件的右键菜单
    var tabMenu1 = new BootstrapMenu("[id^='TBL'] tr [controlType]", {
        fetchElementData: function ($CTElem) {
            var CT = $CTElem;
            return CT;
        },
        actionsGroups: [  //给右键菜单的选项加一个分组，分割线
            ["移除"],
            ["取消"]
        ],
        actions: {
            移除: {
                name: "移除",
                iconClass: "fa-times",
                onClick: function (e) {
                    var cuDynC = $("#mainContent").find("[controltype='div']");
                    cuDynC.append(e);
                    e.addClass("form-group");
                    var parCT = e.attr("controltype");
                    
                    var curWidth = 200;
                    e.width(curWidth);
                    e.css("position", "absolute");
                    //对日期控件特殊处理
                    if (parCT == "date") {
                        e.width(curWidth + 50);
                        e.find("input").css("width", curWidth);
                        e.find("input").css("float", "left");
                        e.find("button").css("display", "");
                        e.css("float", "left");
                    }
                }
            },
            取消: {
                name: "取消",
                iconClass: "fa-add",
                onClick: function () {
                }
            }
        }
    });

    //保存快捷键 ctrl+s
    $(document).keydown(function (event) {
        //ctrl 键盘事件
        if (event.ctrlKey) {
            var objs = $(".control-selected");
            switch (event.keyCode) {
                case 37:                               //快捷键 ctrl+左
                    event.preventDefault();
                    objs.each(function () {
                        SaveTemporHtml();
                        var fullLeft = $(this).css("left").replace("px", "");
                        //var offsetLeft = $(this)[0].offsetLeft;
                        if (fullLeft > 0) {
                            $(this).css("left", (parseInt(fullLeft) - 1) + "px");
                            //$(this)[0].style.left = parseInt($(this)[0].offsetLeft - 1) + "px";
                        }
                    });
                    break;
                case 38:                              //快捷键 ctrl+上
                    event.preventDefault();
                    objs.each(function () {
                        SaveTemporHtml();
                        var fullTop = $(this).css("top").replace("px", "");
                        if (fullTop > 0) {
                            $(this).css("top", (parseInt(fullTop) - 1) + "px");
                        }
                    });
                    break;
                case 39:                               //快捷键 ctrl+右
                    event.preventDefault();
                    objs.each(function () {
                        SaveTemporHtml();
                        var fullLeft = $(this).css("left").replace("px", "");
                        //var offsetLeft = $(this)[0].offsetLeft;
                        if (fullLeft > 0) {
                            $(this).css("left", (parseInt(fullLeft) + 1) + "px");
                            //$(this)[0].style.left = parseInt($(this)[0].offsetLeft + 1) + "px";
                        }
                    });
                    break;
                case 40:                               //快捷键 ctrl+下
                    event.preventDefault();
                    objs.each(function () {
                        SaveTemporHtml();
                        var fullTop = $(this).css("top").replace("px", "");
                        if (fullTop > 0) {
                            $(this).css("top", (parseInt(fullTop) + 1) + "px");
                        }
                    });
                    break;
                case 83:                              //保存快捷键 ctrl+s
                    event.preventDefault();           //屏蔽浏览器自带快捷键
                    $("#btnSave").click();
                    break;
                case 90:                              //撤销快捷键 ctrl+z
                    event.preventDefault();
                    $("#undo").click();
                    break;
                case 89:                              //重做快捷键 ctrl+y
                    event.preventDefault();
                    $("#redo").click();
                    break;
                case 67:
                    event.preventDefault();
                    DoCopy();
                    break;
                case 86:
                    event.preventDefault();
                    DoPaste();
                    break;
                default:
                    break;
            }
        }
    });
});